Forum Moderators: coopster
I'm looking for the best way of sorting a folder list by date modified, with the most recently modified or created at the top, but only list the most recent 3 or 4, not all of them as it may contain millions (slight exageration).
I've been through many searches online but either the code didnt work or wasnt what i was after.
I have 2 different bits of code, one is an array where i cant sort it by date, and the other hasnt got any sorting methods and just lists the folders in whatever order it feels like.
I'm doing this within a while loop, for a bunch of other folders, as its the sub-folders within those folders that im trying to find are the most recently updated.
I'm aware of the filemtime function, but i cant figure out a way of making it work, same with the sort() functions for arrays.
Anyone got any pointers on how to go about this?
function listDirectory($dir) {
$files = Array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.html") {
$size=filesize($dir."/".$file);
$date=filemtime($dir."/".$file);
$files['extension'][]=strtolower(extname($file));
$files['file'][] = $file;
$files['date'][] = $date;
$files['size'][] = $size;
}
closedir($handle);
}
return $files;
} function extname($file) {
$file = explode(".",basename($file));
return $file[count($file)-1];
} load each folder into a single array of files, then you'll need to look at sorting multi-dimensional arrays (there's plenty of threads here about this)
hth
k