PHP: Sort Files from Directory & Order them by Filemtime()
Posted on October 5, 2008, under PHP,
Bookmark it
This is a function that selects files from a directory and orders them by the last time they were changed, in ascending or descending order. The snippet also calculates how much time passed since the file’s content was changed.
function Sort_Directory_Files_By_Last_Modified($dir, $sort_type = 'descending', $date_format = "F d Y H:i:s.")
{
$files = scandir($dir);
$array = array();
foreach($files as $file)
{
if($file != '.' && $file != '..')
{
$now = time();
$last_modified = filemtime($dir.$file);
$time_passed_array = array();
$diff = $now - $last_modified;
$days = floor($diff / (3600 * 24));
if($days)
{
$time_passed_array['days'] = $days;
}
$diff = $diff - ($days * 3600 * 24);
$hours = floor($diff / 3600);
if($hours)
{
$time_passed_array['hours'] = $hours;
}
$diff = $diff - (3600 * $hours);
$minutes = floor($diff / 60);
if($minutes)
{
$time_passed_array['minutes'] = $minutes;
}
$seconds = $diff - ($minutes * 60);
$time_passed_array['seconds'] = $seconds;
$array[] = array('file' => $file,
'timestamp' => $last_modified,
'date' => date ($date_format, $last_modified),
'time_passed' => $time_passed_array);
}
}
usort($array, create_function('$a, $b', 'return strcmp($a["timestamp"], $b["timestamp"]);'));
if($sort_type == 'descending')
{
krsort($array);
}
return array($array, $sort_type);
}
Here’s an usage example of this function:
$dir = '/home/public_html/my_directory/';
$array = Sort_Directory_Files_By_Last_Modified($dir);
// Info Array
$info = $array[0];
// Sort Type
$sort_type = $array[1];
echo '<h3>'.$dir.'</h3>';
echo 'Order by: Last Modified ('.$sort_type.')<br />';
foreach($info as $key => $detail)
{
echo '<h4 style="color: #3B4F9F;">'.$detail['file'].'</h4>';
echo 'Last Modified: '.$detail['date'].'<br />';
$time_passed = '';
foreach($detail['time_passed'] as $type => $value)
{
$time_passed .= $value." ".$type.", ";
}
$time_passed = "<span style='color: #377420;'>".rtrim($time_passed, ", ")."</span> ago";
echo $time_passed."nn";
}
NOTE: If you are using a PHP Version lower than 5, replace the code from the 3rd line (function file):
$files = scandir($dir);
with this one:
$dh = opendir($dir);
$files = array();
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
}
This is a PHP4 alternative to scandir().
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- October 5, 2008
- article by Gabriel C.
- 5 comments
Related Posts
-
PHP: Calculate the Size, Number of Files & Folders of a Directoryat September 24, 2008 with 3 comments
-
How to recursively read a directory with all its contentat September 4, 2008
-
Display Values from an Array in Random Orderat October 2, 2008
-
PHP: Some Ways to Scan a Directoryat December 12, 2008 with 4 comments
-
How to Remove a (non-empty) Directory using PHPat September 28, 2008 with 1 comment

5 Replies to "PHP: Sort Files from Directory & Order them by Filemtime()"
October 17, 2008 at 1:03 PM
Its a superb script. Its working fine. Keep it up.
October 31, 2009 at 5:11 AM
Fantastic! Works straightaway! Thanks a million!
January 21, 2010 at 2:38 AM
error for me
Warning: filemtime() [function.filemtime]: stat failed for
on line 12 of function
$last_modified = filemtime($dir.$file);
January 22, 2010 at 2:11 AM
Thank u so much!
Its working fine
January 29, 2010 at 5:54 PM
Thank you… this is very useful script.