Click to See Complete Forum and Search --> : Sorting files by date.


HomoErectus
10-01-2007, 04:44 AM
$image_dir './images/';
$images = glob($image_dir."*.jpg"));How can I sort the $images array so it has the most recently modified file in first place and least recently modified file in last place?

MrCoder
10-01-2007, 05:11 AM
Build an array of files then sort the array using filemtime() (http://uk2.php.net/manual/en/function.filemtime.php)

HomoErectus
10-01-2007, 05:23 AM
Hello MrCod,

I already have the array of files retrieved using the above code. How would I sort it with filemtime?

NightShift58
10-01-2007, 05:39 AM
Here's an example from the PHP docs, extended somewhat and using the pretty coding style that MrCoder likes and missed...

<?php
$arrDIR = array();
FOREACH (glob("*") as $filename) :
IF (is_file($filename)) : // if you want to omit directories
$arrDIR[$filename] = filemtime($filename);
ENDIF;
ENDFOREACH;
asort($arrDIR);
?>

MrCoder
10-01-2007, 05:50 AM
Here's an example from the PHP docs, extended somewhat and using the pretty coding style that MrCoder likes and missed...

<?php
$arrDIR = array();
FOREACH (glob("*") as $filename) :
IF (is_file($filename)) : // if you want to omit directories
$arrDIR[$filename] = filemtime($filename);
ENDIF;
ENDFOREACH;
asort($arrDIR);
?>


Just like that but with {} :)

bokeh
10-01-2007, 06:48 AM
I would, (surprise, surprise) do it differently.$image_dir './images/';
$images = glob($image_dir."*.jpg"));Just add the following line after your code:array_multisort(array_map('filemtime', $images), SORT_DESC, $images);

NightShift58
10-01-2007, 11:36 PM
So would I if performance was certain to never be an issue.

bokeh
10-02-2007, 02:30 AM
So would I if performance was certain to never be an issue.What performance issue do you see?

NightShift58
10-02-2007, 04:03 AM
Most of these canned array functions are extremely slow.

dericks
02-18-2011, 06:41 AM
I am getting following error

Warning: filemtime() [function.filemtime]: stat failed for...

The sorting code used
array_multisort(array_map('filemtime',$afiles),SORT_DESC,SORT_NUMERIC,$afiles);

Please support find the issue...

Thanks