glob - Finding latest file's name and modification time in PHP -


i search director glob function , matched files' list. checking filemtime of files create map. sort map respect file dates. @ last latest file's name , modification time. code this. works small directories, it's slow big directories. wonder whether there faster/clever way?

$filelist = array(); // $id = argument of function $files = glob('myfolder'.ds.'someone'.$id.'*.txt'); if (empty($files)) {     return 0; } foreach ($files $file) {     $filelist[filemtime($file)] = $file; }  if (sizeof($files) > 1) {     ksort($filelist); }  $latestfilename = end($filelist); $filelastmodifdate = filemtime( $latestfilename ); 

i suspect "mapping" creating hash table, , sort not efficiant 1 might expect, try this: (this basic, u can fancy up)

class file_data {    public  $time; // or whatever u use    public $file; }  $arr =new array (); foreach ($files $file) {      $new_file = new file_data ();      $file_data->time = filetime($file);      $file_data->file = $file;         array_push ($arr,$file_data); }  function file_object_compare ($a,$b) {     if ($a->time > $b->time) return -1; //    etc... i.e 0 eual 1 otherwise  }  //and u sort usort ($arr,"file_object_compare"); 

// or // , better, if u need paricular sorting

function file_object_compare ($a,$b) {     if (filetime($a)> filetime($b)) return -1; //    etc... i.e 0 eual 1 otherwise     }  usort ($files,"file_object_compare"); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -