Forum Moderators: coopster

Message Too Old, No Replies

listing array values by date modified

         

surrealillusions

9:04 pm on Apr 29, 2009 (gmt 0)

10+ Year Member



Hi all,

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?

midtempo

9:21 pm on Apr 29, 2009 (gmt 0)

10+ Year Member



what i'd do is read the folder contents into a database. you'd only need to do this once, then you can add each new item as it's uploaded (or whatever)

do the sort/limit in mysql.

surrealillusions

11:03 am on Apr 30, 2009 (gmt 0)

10+ Year Member



MySQL isnt really an option with this. It would make it a whole lot easier, but it would be adding too much to something that doesnt absolutely need it, plus I cant setup the MySQL database on the site this is going on.

midtempo

11:43 am on Apr 30, 2009 (gmt 0)

10+ Year Member



okay, here's an old function i've used in the past to get an array of files from a dir.


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