Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: None  |  size: 0.86 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get the most recently updated file in PHP
  2. file_12.css
  3. file_34.css
  4. file_14.css
  5.        
  6. int filemtime ( string $filename )
  7.        
  8. string readdir ([ resource $dir_handle ] )
  9.        
  10. array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )
  11.        
  12. <?php
  13.  
  14. $files = array();
  15.  
  16. if ($handle = opendir('/path/to/files')) {
  17.     while (false !== ($file = readdir($handle))) {
  18.         if (is_file($file)) {
  19.             $modified = filemtime($file);
  20.             $files[$modified] = $file;
  21.         }
  22.     }
  23.     closedir($handle);
  24. }
  25.  
  26. krsort($files);
  27.  
  28. $last_modified_file = array_shift($files);
  29.        
  30. function cmp($a, $b)
  31. {
  32. if(!is_file($b) || !strpos('file_', $b)) return -1;
  33. $a = filemtime($a);
  34. $b = filemtime($b);  
  35. return  ($a == $b) ? 0 :( ($a > $b) ? -1 : 1 );
  36. }
  37. usort($handle = scandir('/path/to/files'), "cmp");
  38. $file = $handle[0];
  39. unset($handle);