Archive

Posts Tagged ‘glob’

Sequentially Rename All Image Files In A Directory With PHP

March 2nd, 2009 1 comment

The following function will rename all of the image files in a directory to be sequential. The parameters are the path of the directory that the files are in and the name of a function that will be used to sort the array of files through the PHP usort() funciton.

function sequentialImages($path, $sort=false) {
 $i = 1;
 $files = glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT);
 
 if ( $sort !== false ) {
  usort($files, $sort);
 }
 
 $count = count($files);
 foreach ( $files as $file ) {
  $newname = str_pad($i, strlen($count)+1, '0', STR_PAD_LEFT);
  $ext = substr(strrchr($file, '.'), 1);
  $newname = $path.'/'.$newname.'.'.$ext;
  if ( $file != $newname ) {
   rename($file, $newname);
  }
  $i++;
 }
}

The following function can be used in the second parameter to sort the files by their last modified time.

function sort_by_mtime($file1, $file2) {
 $time1 = filemtime($file1);
 $time2 = filemtime($file2);
 if ( $time1 == $time2 ) {
  return 0;
 }
 return ($time1 < $time2) ? 1 : -1;
}

Putting these two function together we can call the sequentialImages() function like this.

sequentialImages('files','sort_by_mtime');

This function takes the following set of images:

file1.gif
file2.gif
wibble.gif
wobble.gif
02.gif

And renames them to the following:

01.gif
02.gif
03.gif
04.gif
05.gif

Categories: PHP Tags: , , , , , , , , , ,

The glob() Function In PHP

March 25th, 2008 No comments

The glob() function in PHP uses simple pattern matching to find files and directories in a directory and return those file names as an array. It simplifies all of the PHP directory functions, so instead of opening the directory and then reading every file name one by one, you can just use glob() and so it in one function call. Additionally, glob() works very closely with the file system and so it very quick.

Here is a simple example.

$globOut = glob('*');

The $globOut variable now contains a list of all of the files and directories in the root folder. If you want to find all of the files ending with HTML in a directory then use the following.

$globOut = glob('*.html');

The function takes another parameter which is a constant that defines some ways of changing the output. Here is a list of the available constants.

  • GLOB_MARK : Adds a slash to each item returned.
  • GLOB_NOSORT : Return files as they appear in the directory (no sorting).
  • GLOB_NOCHECK : Return the search pattern if no files matching it were found.
  • GLOB_NOESCAPE : Backslashes do not quote metacharacters.
  • GLOB_BRACE : Expands {a,b,c} to match ‘a’, ‘b’, or ‘c’.
  • GLOB_ONLYDIR : Return only directory entries which match the pattern.
  • GLOB_ERR : Stop on read errors (like unreadable directories), by default errors are ignored.

If you wanted to find all of the files in the root folder that end in either .php or .html then you can use the GLOB_BRACE constant in the following way.

$globOut = glob('*.{php,html}',GLOB_BRACE);

In this example the string ‘*.{php,html}’ is split into *.php and *.html and so the function matches both file types.

There are numerous different things that can be done with the function, for example, to print out a list of all of the HTML files in a directory as a list of links use the following little snippet.

$globOut = glob('*.html');
if(count($globOut)>0){ // make sure the glob array has something in it
 foreach ($globOut as $filename){
  echo '<a href="'.$filename.'" title="'.$filename.'">'.$filename.'</a><br />';
 }
}else{
 echo 'No files found.<br />';
}

In a recent application I had a directory of files that each had some debugging information in them that were created during important function calls. Each file was names with a timestamp and a .html to say when the file was created. I wanted to create a bit of code that would allow me to view the files easily as a table, with the most recently created file at the top.

$globOut = glob('debug/*.html');
if(count($globOut)>0){
 $globOut = array_reverse($globOut);
 echo '<table;><tr><th>File</th><th>Size</th><th>Timestamp</th></tr>';
 foreach($globOut as $filename){
  echo '<tr><td><a href="'.$filename.'" title="'.$filename.'">'.$filename.'</a></td><td>'.filesize($filename).'</td><td>'.date('l dS \of F Y h:i:s A',str_replace(array('debug/','.html'),array('',''),$filename)).'</td></tr>';
 }
 echo '</table>';
}else{
 echo '<p>No debug files found.</p>';
}

One thing to be careful of is that there are some compatibility issues with the glob() function on some systems, especially PHP 4 and earlier. So you might want to check that it works or at least create an alternative before relying on it.

Categories: PHP Tags: , , , , ,