Web Hosting Talk







View Full Version : Listing files by date added


Justice
11-04-2004, 06:04 PM
Right now, I have a piece of code set up to display a list of mp3's in a directory, but I can't control the order it's displayed in yet. I want the files to be listed from newest to oldest.

<?php

$cnt = 1;

if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && !is_dir($file) && fnmatch("*.mp3", $file)) {
$desc = strtr($file, "_", " ");
$desc = str_replace(".mp3", '', $desc);
$desc = ucwords(strtolower($desc));
if(fmod($cnt, 2) == 0) $color = "#E2E2E2";
else $color = "#FFFFFF";

echo "<tr><td bgcolor='$color'><a href='$file'>$desc</a></td><td bgcolor='$color' align='center'>"
.round((filesize($file) / 1024), 0)." KB</td><td bgcolor='$color' align='center'>"
.date ("m.d.Y", filemtime($file))."</td></tr>";

$cnt++;
}
}
closedir($handle);
}

?>
Is there a way to edit that code to control the list, or do I need to use a completely different approach?

Thanks in advance for any help.

Justice
11-04-2004, 07:08 PM
found the solution..n/m

azizny
11-04-2004, 07:12 PM
Really nice script.

was looking for it for a while.

you just have to mess around with the filetime($file).

Peace,

xgoth3
11-04-2004, 07:34 PM
I'm curious, how did you made it work properly?

tiamak
11-04-2004, 11:23 PM
Originally posted by xgoth3
I'm curious, how did you made it work properly?

simple :) first create array lets say $a then add elements to it - replace echo with $a[filemtime($filename)] =

then at the end of this: arsort() array and reset() it and then use some loop to output already sorted results from array :)


something like this :) - i am not sure if arsort is good function in this example :)