Click to See Complete Forum and Search --> : Sorting folders by 'last modified'


darkestnight
12-14-2004, 11:58 AM
I have a script which lists folder names (and does something to them for an image gallery). Is there a way I can adapt my script so it displays the folders in the order of 'last modified' so the most recently modified folder is the first in the list?

Thank you in advance!

Here's my current PHP. This lists the folders (and does something to them) but does not order it by 'last modified'.

<?php
$d = opendir('images');
while (($file = readdir($d)) !== false) {
if ((filetype("images/$file") == 'dir') && (substr($file,0,1) != '.')) {
$nfile = str_replace('_',' ',$file);
print "<li><a href=\"gallery.php?gallery=$file\">$nfile</a></li>";
}
}
?>

ShrineDesigns
12-14-2004, 02:35 PM
try this<?php
$files = array();
$mtime = array();
$dh = opendir('images');

while(($file = readdir($dh)) !== false)
{
if(filetype("images/$file") == 'dir' && ($file != '.' && $file != '..'))
{
$files[] = $file;
$mtime[] = date('U', filemtime("images/$file"));
}
}
closedir($dh);
// clear the cache php created
clearstatcache();

array_multisort($files, SORT_ASC, SORT_STRING, $mtime, SORT_ASC, SORT_NUMERIC);

foreach($files as $file)
{
$nfile = str_replace('_', ' ', $file);
echo "<li><a href=\"gallery.php?gallery=$file\">$nfile</a></li>";
}
?>

darkestnight
12-14-2004, 02:42 PM
I thought it was ok, but it only puts it in alphabetical order! :(

darkestnight
12-15-2004, 09:41 AM
Its ok, my friend helped me.

ShrineDesigns
12-15-2004, 12:13 PM
sorry about that, the examples give by the php manual were not very good, try this:<?php
$files = array();
$mtime = array();
$dh = opendir('images');

while(($file = readdir($dh)) !== false)
{
if(filetype("images/$file") == 'dir' && ($file != '.' && $file != '..'))
{
$files[] = $file;
$mtime[] = date('U', filemtime("images/$file"));
}
}
closedir($dh);
// clear the cache php created
clearstatcache();

arsort($mtime, SORT_NUMERIC);
reset($mtime);

while(list($k, $v) = each($mtime))
{
$file = $files[$k];
$nfile = str_replace('_', ' ', $file);
echo "<li><a href=\"gallery.php?gallery=$file\">$nfile</a></li>";
}
?>