
Untitled
By: a guest on
May 31st, 2012 | syntax:
None | size: 0.86 KB | hits: 12 | expires: Never
Get the most recently updated file in PHP
file_12.css
file_34.css
file_14.css
int filemtime ( string $filename )
string readdir ([ resource $dir_handle ] )
array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )
<?php
$files = array();
if ($handle = opendir('/path/to/files')) {
while (false !== ($file = readdir($handle))) {
if (is_file($file)) {
$modified = filemtime($file);
$files[$modified] = $file;
}
}
closedir($handle);
}
krsort($files);
$last_modified_file = array_shift($files);
function cmp($a, $b)
{
if(!is_file($b) || !strpos('file_', $b)) return -1;
$a = filemtime($a);
$b = filemtime($b);
return ($a == $b) ? 0 :( ($a > $b) ? -1 : 1 );
}
usort($handle = scandir('/path/to/files'), "cmp");
$file = $handle[0];
unset($handle);