Locating a specific file inside a ZIP archive

Here's a quick way to locate a specific file in a ZIP archive.  The example below shows how to locate anything with a .csv file extension.  This example could be modified to locate a specific file name via an exact string match (ex:  if($zip->getNameIndex($i) == "SomeString.txt")), or as shown below, you can use a regex match.

$zip = new ZipArchive;
if($zip->open($zipFileName) === TRUE) {
    for($i = 0;$i < $zip->numFiles;$i++) {
        if(preg_match("|\.csv$|i", $zip->getNameIndex($i))) {
            $fileToExtract = $zip->getNameIndex($i);
        }
    }
    $zip->extractTo('/path/to/extraction/directory/', $filetoExtract);
    $zip->close();
}

February 2, 2012 at 12:07pm / 0 comments / PHP 

Verbose Unix commands by default (rm/cp/mkdir/etc)

If you want to see output of what these commands is doing, you can add the optional "-v" flag.

Or, you can add this to your .bash_profile (or whatever shell you're using)

alias cp='/bin/cp -v'
alias mv='/bin/mv -v'
alias rm='/bin/rm -v'
alias mkdir='/bin/mkdir -v'
alias rmdir='/bin/rmdir -v'

 

November 16, 2011 at 2:03pm / 1 comments / Linux 

PHP function to create SEO friendly URL, Unix friendly file names

This is a useful function for creating SEO friendly URL's.  It's also handy for creating Unix friendly file names.  

It simply removes anything that isn't a number or a letter and replaces it with a hypen (or any other character you pass... such as an underscore.) 

function seome($str, $char = '-')
{
  // convert non-alphanumeric to dashes
  $str = preg_replace("([^A-Za-z0-9])", $char, $str);

  // replace 2 or more dashes with single dashes
  $str = preg_replace("/(-){2,}/m", $char, $str);

  // remove any trailing or leading dashes
  $str = trim($str, $char);

  return $str;
}

November 6, 2011 at 4:47pm / 1 comments / Code, General Web Programming, PHP 

Easy way to check if HTTPS / SSL is being loaded (PHP)

<?php 

function httpscheck($url = '')
{
  if($_SERVER['HTTPS'] != "on" || $_SERVER['HTTP_PORT'] != 443)
  {
    if(strlen($url) == 0)
    {
      $url = $_SERVER['REQUEST_URI'];
    }
    if(headers_sent())
    {
      trigger_error('SSL redirect failed as HTTP headers were previously sent to the browser', E_NOTICE);
    }
    else
    {
      die(header('Location: ' . $url));
    }
  }
}

October 21, 2011 at 3:55am / 1 comments / Code, PHP 

PHP - simple cookie checking script

<?php

if(isset($_GET['set']) && $_GET['set'] != 'yes')
{
  // Set cookie
  setcookie('test', 'test', time() + 60);

  // Reload page
  header("Location: cookie.php?set=yes");
  exit ;
}
else
{
  // Check if cookie exists
  if(isset($_GET['set']) && ! empty($_COOKIE['test']))
  {
    echo "Cookies are enabled on your browser";
  }
  else
  {
    echo "Cookies are <b>NOT</b> enabled on your browser";
  }
}
?>

October 21, 2011 at 3:53am / 1 comments / Code, PHP 

cURL error codes PHP array

<?php

$curl_errors = array();
$curl_errors[0] = "CURL_OK";
$curl_errors[1] = "CURL_UNSUPPORTED_PROTOCOL";
$curl_errors[2] = "CURL_FAILED_INIT";
$curl_errors[3] = "CURL_URL_MALFORMAT";
$curl_errors[4] = "CURL_URL_MALFORMAT_USER";
$curl_errors[5] = "CURL_COULDNT_RESOLVE_PROXY";
$curl_errors[6] = "CURL_COULDNT_RESOLVE_HOST";
$curl_errors[7] = "CURL_COULDNT_CONNECT";
$curl_errors[8] = "CURL_FTP_WEIRD_SERVER_REPLY";

?>

October 21, 2011 at 3:51am / 1 comments / Code, PHP 

prevent browser caching with PHP

<?php

function bustcache()
{
  if(headers_sent())
  {
    trigger_error('Already sent HTTP headers, not busting the cache', E_NOTICE);
  }
  else
  {
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
  }
}

?> 

The above is a very simple function for preventing a browser from caching content.  This isn't recommended on static sites, but is exceptionally helpful when building dynamic applications. Simply call this function before any output.

October 21, 2011 at 3:48am / 1 comments / Code, General Web Programming, PHP 

how to fix the broken edit button in shutter

Shutter is one of my favorite Linux programs. It does what Windows' Snipping Tool does, plus a lot more.

One of the functions I really like is being able to edit screen shots; annotation, arrows, circles, whatever you need to illustrate the screenshot.

Recently I upgraded a machine to Ubuntu 10.04 and found that the edit button was grayed out --- Shutter would no longer let me edit any screenshots.

Thankfully, the fix was simple:

sudo apt-get install libgoo-canvas-perl

October 18, 2011 at 5:42am / 1 comments / Linux, Ubuntu 

ways to make your website easier to maintain using php

This is nowhere near an all inclusive list of things you can do to make your life easier when having to maintain a website.  These are just a few tips and tricks worth knowing.

1) Never use absolute paths.  Use relative paths instead.

For example, this is a no-no:

include('/home/user/public_html/some/file');

Instead, try this:

include($_SERVER['DOCUMENT_ROOT'] . '/some/file');

Or even this:

include('../../some/file');

Then if (when!) you migrate to a new server, and your base web dir changes, you won't have to update your file paths throughout your code.

2) Use a config file.

Don't hard code your email address throughout your site.  define() it in a config file that you include() everywhere.  Same goes for mysql credentials, or any other text which may need to change at some point.  Putting everything in an external config makes a migration much easier.

Example:

define('EMAIL','my@email.com');
define('DB_USER','mydbusername');
define('DB_PASS','mydbpassword'); 

Or, use global variables:

$GLOBALS['EMAIL'] = 'my@email.com';
$GLOBALS['DB_USER'] = 'mydbusername';
$GLOBALS['DB_PASS'] = 'mydbpassword'; 

3) Place re-used web page code into external files. include() them.

Got a navigation menu that appears on every page?  Place it into a file (e.g. navigation.php) and include() it on every page.

Google Analytics?  Same deal--- include() it.

Doing so makes site maintenance MUCH easier.


October 10, 2011 at 8:22am / 1 comments / General Web Programming, Perl, PHP 

How to tell if PHP is running from the command line (CLI) or the web.

if(isset($_SERVER['REMOTE_ADDR']))
{
    die();
}

If you have a script that needs to run from the command line (say, via cron), but for some dumb reason needs to reside in a publicly accessible location (e.g. via the web), use the above code at the very start of your script.  The only time that particular variable exists is when someone is connecting over the web.  Command line execution doesn't create it.

 

October 6, 2011 at 10:26pm / 2 comments / PHP