I have a function like so: if ($handle = opendir('layouts')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "css" && $file != "..") { $files[] = $file; } } closedir($handle); } $array_lowercase = array_map('strtolower', $files); array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $files); PHP: and then I use a for loop to echo each of the files for($e=$perpage*($page-1);$e<$last;$e++){ PHP: How could I get it to display the latest 20 files added by file date? At the moment its in alphabetical order Cheers
Try this. $filetimes = array_map('filectime', $files); array_multisort($filetimes, SORT_NUMERIC, SORT_DESC, $files); unset($filetimes); PHP: $files should not be sorted by file creation time, in descent order.
I'm getting a huge error listing for all the files like so: Warning: filectime() [function.filectime]: Stat failed for image-of-tigers.jpg (errno=2 - No such file or directory) in
This is what I got now <?php if ($handle = opendir('layouts')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "css" && $file != "..") { $files[] = $file; } } closedir($handle); } $array_lowercase = array_map('strtolower', $files); $filetimes = array_map('filectime', $files); array_multisort($filetimes, SORT_NUMERIC, SORT_DESC, $files); unset($filetimes); PHP: it prints out lines of that error, and the displays them as they normally would without the added code
My bad, I forgot you didn't include the directory names in the $files array. This does work. (Tested) if ($handle = opendir('layouts')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "css" && $file != "..") { $files[] = "layouts/{$file}"; } } closedir($handle); } $filetimes = array_map('filectime', $files); array_multisort($filetimes, SORT_NUMERIC, SORT_DESC, $files); unset($filetimes); // Get rid of directory names $files = array_map('basename', $files); print_r($files); PHP:
Works a treat, I figured thats what was going on but didn't know how to implement it inject layouts/ before each value in the array. Cheers nico, you've come to the rescue. Yet again