tags:

views:

200

answers:

3

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);
}
+7  A: 

This would get all files in path/to/files with an .swf extension into an array and then sort that array by the file's mtime

$files = glob('path/to/files/*.swf');
usort($files, function($a, $b) {
    return filemtime($a) < filemtime($b);
});

The above uses an Lambda function and requires PHP 5.3. Prior to 5.3, you would do

usort($files, create_function('$a,$b', 'return filemtime($a)<filemtime($b);'));

If you don't want to use an anonymous function, you can just as well define the callback as a regular function and pass the function name to usort instead.

With the resulting array, you would then iterate over the files like this:

foreach($files as $file){
    printf('<tr><td><input type="checkbox" name="box[]"></td>
            <td><a href="%1$s" target="_blank">%1$s</a></td>
            <td>%2$s</td></tr>', 
            $file, // or basename($file) for just the filename w\out path
            date('F d Y, H:i:s', filemtime($file)));
}

Note that because you already called filemtime when sorting the files, there is no additional cost when calling it again in the foreach loop due to the stat cache.

Gordon
thanks for this answer.
sasori
-1 You don't utilize his code and usort is the worst sort function to use in that case. But hell, all vote for you because it looks fancy. *-*
elias
@elias No offense, but I find it rather ridiculous to downvote a perfectly valid answer for the reasons you gave. Talking about readability your code takes much longer to figure out than mine. I'd call it Spaghetti code. And if you are unhappy about usort being oh so terrible, than file a complaint with PHP's bugtracker.
Gordon
@elias You got the green tick, so take it easy... ;)
zaf
@Gordon I've just added logic to his code. My own code would look different. But probably i'm just frustrated that people see a closure and upvote like mad.
elias
@elias It's a Lambda, not a Closure. And I am pretty sure that's not the only reason the answer got upvoted. I also fail to see why I should expand on poor code, when I know the code to improve it.
Gordon
@Gordon There is no spoon!
elias
@elias So why do you try to bend it then? ;)
Gordon
@Gordon I did, but you can't see. :P
elias
+1  A: 
elias
thank you sir, this is the best answer as far as I can see, I just tested it. Thank you ^:)^
sasori
your welcome, ty for accept! :)
elias
Gordon
thank you very much for the tip sir. I'll take note of that ^:)^
sasori
@sasori also mind that elias solution as it is given now will not work properly when you've got files with the same mtime, as it will overwrite the previous filename for that mtime.
Gordon
+1  A: 

If you are using PHP5 then this is the kind of job you can do easier using the SPL's DirectoryIterator.

http://php.net/manual/en/class.directoryiterator.php

Google DirectoryIterator to see some tutorials on how to use this too.

Cups
thanks for tip ^:)^
sasori