sort files by date in PHP

Posted by sasori on Stack Overflow See other posts from Stack Overflow or by sasori
Published on 2010-04-19T11:47:56Z Indexed on 2010/04/19 11:53 UTC
Read the original article Hit count: 237

Filed under:
|

hi, I currently have an index.php file which allows me to output the list of files inside the same directory, the output shows the names then I used filemtime() function to show the date when the file was modified. my problem now is, how will I sort the output to show the latest modified file ?, I've been thinking for awhile how to do this. if only I am doing it with mysql interaction there will be no problem at all. please show me an example how to sort and output the list of files starting from the latest modified one. this is what i have for now

if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
        $lastModified = date('F d Y, H:i:s',filemtime($file));
          if(strlen($file)-strpos($file,".swf")== 4){
            echo "<tr><td><input type=\"checkbox\" name=\"box[]\"></td><td><a href=\"$file\" target=\"_blank\">$file</a></td><td>$lastModified</td></tr>";
           }
       }
   }
   closedir($handle);
}

© Stack Overflow or respective owner

Related posts about php

Related posts about sorting