Ars Informatica
December 14, 2017
Home
Health Care Informatics
Web-based MySQL/PHP Databasing
Programming
Web Development
Favourite Software
Hardware for the Frugal Fanatic
Graphic Design and Image Processing
Free Scripts and Software
About Us
Contact Us
Links
 
 

 Article Feed for this site

Reading and sorting a file directory using PHP

January 13, 2009

Reading and sorting a server-side file directory is not difficult - but something we do often enough that some generic cut-and-paste code seems warranted.

The first part is specifying the directory path, $dir, and referencing the directory itself via the directory handle, $dh.

Then, we walk the directory structure, using function readdir(). The simplest version of this code displays sequential file names only:

$dir = 'c:/some_directory';
     
if ($dh = opendir($dir)) {
     while ($file = readdir($dh)) echo "$file<br>\n";
     closedir($dh);
}

 
In a slightly fancier version, I use array $exclude_files to exclude any filenames I don't want to capture or show - for example that of the parent directory (.), of the current directory (..), or of certain security-sensitive files (i.e. .htaccess). The remaining results I log to array $files.

$dir = 'c:/some_directory';
$exclude_files = array('.', '..');
$files = array();

if ($dh = opendir($dir)) {
     while ($file = readdir($dh)) if (!in_array($file, $exclude_files))
          $files[] = $file;
     closedir($dh);
}

echo '<pre>';
print_r($files);
echo '</pre>';

The last three lines simply display the $files array contents - I will not handle formatting the output here, as dozens of variations are possible.

 
I use the sort parameter as the key for array $files - specified as $sort_key in this code. This code recognizes values file_name, file_size, file_type, and date_modified. $sort_order determines whether sorting should go alphanumeric, or from large to small, or most recent to oldest, etc. Valid values are ESC and DESC. The default is to go from A to Z, small to large, old to new ...

$dir = 'c:/some_directory';
$exclude_files = array('.', '..');
$sort_key = 'date_modified';
$sort_order = 'DESC';
$files = array();

$dir = "$dir/";
if ($dh = opendir($dir)) {
     while ($file = readdir($dh))  if (!in_array($file, $exclude_files)) {
          $file_size = filesize($dir.$file);
          $file_type = is_dir($dir.$file) ? 'directory' : 'file';
          $file_mtime =  filemtime($dir.$file);
          $date_modified = date("d/m/Y g:i:s A", $file_mtime);

          if ($sort_key == 'file_name') $files[$file] =
               array($file, $file_size, $file_type, $date_modified);
          else if ($sort_key == 'file_size') $files[$file_size] =
               array($file, $file_size, $file_type, $date_modified);
          else if ($sort_key == 'file_type') $files[$file_type] =
               array($file, $file_size, $file_type, $date_modified);
          else if ($sort_key == 'date_modified') $files[$file_mtime] =
               array($file, $file_size, $file_type, $date_modified);
     }
     closedir($dh);
}

if (strtolower($sort_order) == 'desc') krsort($files);
else ksort($files);

Note that the $file_type code is extremely simple. It can be easily enhanced using the function mime_content_type(), if implemented/enabled. If not, custom code would have to be written - the PHP filetype() function is useless in this capacity.

Copyright © 2017 Ars Informatica. All Rights Reserved.