PHP sort files by time


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function sortByChangeTime($file1, $file2)
  2. {
  3. return (filectime($file1) < filectime($file2));
  4. }
  5. $files = glob('*.*'); // use scandir if you want hidden files too
  6. usort($files, 'sortByChangeTime'); // sort by callback
  7. var_dump($files); // dump sorted file list
  8.  
  9. //ALTERNATIVE VERSION
  10.  
  11. if($h = opendir($dir)) {
  12. $files = array();
  13. while(($file = readdir($h) !== FALSE)
  14. $files[] = stat($file);
  15.  
  16. // do the sort
  17. usort($files, 'your_sorting_function');
  18.  
  19. // do something with the files
  20. foreach($files as $file) {
  21. echo htmlspecialchars($file);
  22. }
  23. }

URL: http://stackoverflow.com/questions/2325650/sorting-files-by-creation-modification-date-in-php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.