nikneven Posted June 17, 2009 Share Posted June 17, 2009 Hi, I was wondering if anyone could help me with the following code: function get_oldest_file($directory) { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { $files[] = $file; } foreach ($files as $val) { if (is_file($directory.$val)) { $file_date[$val] = filemtime($directory.$val); } } } closedir($handle); asort($file_date, SORT_NUMERIC); reset($file_date); $oldest = key($file_date); return $oldest; } echo get_oldest_file("./testDirectory/"); As it is right now, it returns the oldest file based on creation, not modification, and it is driving me batty. I would like it to return the oldest file based on modification, and i would like it to display the list of files sorted from oldest to newest with their respective time stamps. I've looked at ksort, and timestamps, and I am so very lost, and would greatly appreciate any help. Thanks again, ~nik Link to comment https://forums.phpfreaks.com/topic/162626-displaying-a-list-of-sorted-files-oldest/ Share on other sites More sharing options...
ldougherty Posted June 17, 2009 Share Posted June 17, 2009 I've found this code on another site and it works. <?php $i = 0; if ($handle = opendir('./')) { //specify path in opendir while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && !is_dir($file)) { $files[$i] = strtotime(date ("M d Y H:i:s", filemtime($file)))." $file"; $i++; } } closedir($handle); } //sorts from increasing(date) use arsort for decreasing(year) sort($files,SORT_NUMERIC); $i=0; foreach ($files as $index => $value) { $modified[$i] = substr($value,11)." was last modified: ".date ("F d Y H:i:s",substr($value,0,20)); $i++; } //prints result as array foreach ($modified as $val) { echo "$val<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/162626-displaying-a-list-of-sorted-files-oldest/#findComment-858372 Share on other sites More sharing options...
nikneven Posted June 17, 2009 Author Share Posted June 17, 2009 Hrmm, that looks like it could work well. Any ideas on how to add to it so that it returns the oldest file as a variable? Link to comment https://forums.phpfreaks.com/topic/162626-displaying-a-list-of-sorted-files-oldest/#findComment-858375 Share on other sites More sharing options...
nikneven Posted June 18, 2009 Author Share Posted June 18, 2009 Actually, that's returning that they were all created at the same time in 1969.... xt was last modified: December 31 1969 19:00:00 was last modified: December 31 1969 19:00:00 was last modified: December 31 1969 19:00:00 was last modified: December 31 1969 19:00:00 t was last modified: December 31 1969 19:00:00 was last modified: December 31 1969 19:00:00 was last modified: December 31 1969 19:00:00 was last modified: December 31 1969 19:00:00 t was last modified: December 31 1969 19:00:00 t was last modified: December 31 1969 19:00:00 Link to comment https://forums.phpfreaks.com/topic/162626-displaying-a-list-of-sorted-files-oldest/#findComment-858539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.