gibbo1715 Posted May 1, 2010 Share Posted May 1, 2010 Hi All Still pretty new to PHP so I hope this isnt too much of a daft question I need to loop through some files and return the result with the most recently created first, I think i ve found a way to list the items by date order but I cant figure out how to return just the file name, might also be that im returning the values with the oldest first actually, not checked yet, Can anyone assist please thanks gibbo <?php $path = './js/'; $list = listdir_by_date($path); foreach($list as $key => $value) { print "$key: $value<br>"; } function listdir_by_date($path){ $dir = opendir($path); $list = array(); while($file = readdir($dir)){ if ($file != '.' and $file != '..'){ // add the filename, to be sure not to // overwrite a array key $ctime = filectime($path . $file) . ',' . $file; $list[$ctime] = $file; } } closedir($dir); krsort($list); return $list; } ?> Link to comment https://forums.phpfreaks.com/topic/200357-sorting-files-by-date-and-returning-just-the-file-name/ Share on other sites More sharing options...
ChemicalBliss Posted May 1, 2010 Share Posted May 1, 2010 krsort($list); will sort $list from newest to oldest. Im not sure what you mean you mean by I cant figure out how to return just the file name Just echo/print $value rather than $key. -cb- Link to comment https://forums.phpfreaks.com/topic/200357-sorting-files-by-date-and-returning-just-the-file-name/#findComment-1051459 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 Do files store the data for date creation? I think it's just last accessed/last modified. If you're on a Linux server, you can easily run the command: ls -alt to list them descendingly and ls -ralt for ascending. Link to comment https://forums.phpfreaks.com/topic/200357-sorting-files-by-date-and-returning-just-the-file-name/#findComment-1051466 Share on other sites More sharing options...
gibbo1715 Posted May 1, 2010 Author Share Posted May 1, 2010 Thankyou for your replies, it is currently on a linux server but may end up on a microsoft one, Last modified is also fine for me so i d be interested if there is a way to order by either of those in a different way. Many Thanks for your help Gibbo Link to comment https://forums.phpfreaks.com/topic/200357-sorting-files-by-date-and-returning-just-the-file-name/#findComment-1051468 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 Let's say you have all the filenames in the array $files. So your job is to get all the file names and store then into that array. Make sure they have the correct path of the file so that filemtime can reference it. <?php function LastModifiedCompare ($a, $b) { return filemtime($a) > filemtime($b); } usort($files, 'LastModifiedCompare'); var_dump($files); // list all of them starting from most recent modified to last. Link to comment https://forums.phpfreaks.com/topic/200357-sorting-files-by-date-and-returning-just-the-file-name/#findComment-1051471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.