reverse chrological order

Hey guys, I'm using the following script to pull images out of a folder and throw them onto a website to quickly.  This script works GREAT, but, is there a way to modify this so that it pulls the pics out in reverse chronological order, IE newest first then older:

if (isset($_GET['folder']) && in_array($_GET['folder'],$folders))
{
$path = "./images/gallery/$folder";
$dir_handle = @opendir($path) or die("Unable to open $path");
while (false !== ($file = readdir($dir_handle)))
      {
    if($file == ".")
    continue;
    if($file == "..")
    continue;
    if($file == "Thumbs.db")
    continue;
    echo "<img src=\"./$path/$file\" width=100 height=100><br />";
      echo "<a href=\"./$path/$file\">$file</a><br />";
      }
closedir($dir_handle);
}

SHSUFLIGHTAsked:
Who is Participating?
 
hernst42Commented:
You need first to collect also the date-times vi filemtime and use array_multisort to sort that result.

Try this (untested):

$files = array();
$times = array();
while (false !== ($file = readdir($dir_handle)))
     {
    if($file == ".")
    continue;
    if($file == "..")
    continue;
    if($file == "Thumbs.db")
    continue;
    $files[] = "./$path/$file";
    $mtime[] = filemtime("./$path/$file");
}
array_multisort($files, SORT_NUMERIC, SORT_DESC, $mtime);

foreach($files as $file) {
    echo "<img src=\"$file\" width=100 height=100><br />";
    echo "<a href=\"$file\">$file</a><br />";
}
0
 
SHSUFLIGHTAuthor Commented:
Spiffy, worked like a charm!!!

Thanks much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.