Forum Moderators: coopster

Message Too Old, No Replies

scandir order by date

         

electricocean

11:33 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



Hi, is there a way to do scandir and order by last file put in the folder (like newsest to oldest)?

thank,
electricocean

coopster

3:38 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The second parameter of the function is the sort order.

array scandir [php.net] ( string directory [, int sorting_order [, resource context]] )

Note, the array will contain filenames as well as directory names. You would still need to test each entry to confirm it actually is a file.

electricocean

1:27 am on Sep 20, 2005 (gmt 0)

10+ Year Member



i am just looking for the number or the thing to write in the order part of the function...

pnllan

8:46 am on Sep 20, 2005 (gmt 0)

10+ Year Member



If you believe the PHP manual, then NO...there is no way to sort by date using ONLY SCANDIR.

The SORT param is alpha ascending (default), or inserting "1" as the param forces alpha descending,

electricocean

4:24 am on Sep 22, 2005 (gmt 0)

10+ Year Member



then how would get a whole list of files in a directory sorted by date created?

coopster

7:40 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You will need to read the files in the directory into an array and get the filemtime [php.net] of each. Then sort the array by the filemtime.

electricocean

7:41 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



can you explain that a little more?

is scandir for php5? i have php4 on my server

dmmh

8:37 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



I think this should work:

<?
$path = '../images/temp/';
# Initialise list arrays, files and array counters for them
$f = 0;
$images_arr = array();
if (@$handle = opendir($path)) {
while (false!== ($file = readdir($handle))) {
if($file!= "." && $file!= ".." && $file[0]!= ".") {
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) $nr_images++;
$images_arr_ftime[$f++] = filemtime($file); //<---- here it is, just a seperate array to store filetimes
$images_arr[$f++] = $file;
};
};
closedir($handle);
};
asort( $images_arr ); reset( $images_arr );
asort( $images_arr_ftime ); reset( $images_arr_ftime );

//test
for ($i=0; $i<$nr_images; $i++){
echo 'filename: '$images_arr[$i].' filetime: '.$images_arr_ftime[$i];
}
?>

dmmh

8:38 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



havent tested tbh, but oughtha work I guess

dmmh

8:37 am on Sep 26, 2005 (gmt 0)

10+ Year Member



a version that actually works:

<?
//phpinfo()
$path = 'images';
# Initialise list arrays, files and array counters for them
$t = 0;
$f = 0;
$images_arr['name'] = array();
$images_arr['time'] = array();
if (@$handle = opendir($path)) {
while (false!== ($file = readdir($handle))) {
if($file!= "." && $file!= "..") {
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)){
$images_arr['time'][$t++] = filemtime($file); //<---- here it is, just a seperate key in the array to store filetimes
$images_arr['name'][$f++] = $file;
};
};
};
closedir($handle);
asort( $images_arr['time'] );
asort( $images_arr['name'] );
}

//test
foreach ($images_arr['time'] as $key=>$ftime){
$fname = $images_arr['name'][$key];
echo '<strong>filename:</strong> '.$fname.' <strong>filetime:</strong> '.$ftime.'<br/>';
}?>