I search a director with glob
function and get the matched files' list. Then by checking filemtime
of the files I create a map. Then sort the map with respect to file dates. At last I get latest file's name and modification time. My code is like this. It works well for small directories, but it's slow for big directories. I wonder whether is there a faster/clever way?
$fileList = array();
// $id = is the argument of this function
$files = glob('myfolder'.DS.'someone'.$id.'*.txt');
if (empty($files)) {
return 0;
}
foreach ($files as $file) {
$fileList[filemtime($file)] = $file;
}
if (sizeof($files) > 1) {
ksort($fileList);
}
$latestFilename = end($fileList);
$fileLastModifDate = filemtime( $latestFilename );