Forum Moderators: coopster

Message Too Old, No Replies

putting filenames of directory in array according to added date

         

bramcorleone

9:18 pm on Jan 16, 2008 (gmt 0)

10+ Year Member



I have a script that puts the name of files (pictures) from a directory in an array. It did put those, that were added to the directory first, at the beginning of the array. After deleting some files and placing a new file in the directory it will be placed almost at the last place in the array.

I didn't use any special code to get them in the order I prefer, it just did.

This is the piece of code:

if($handle=opendir('images/thumbs')){

// Prefix extension for thumbnails directory:
$thumb_prefix="images/thumbs/";

$i=0;

// Go through directory:
while(false!==($file=readdir($handle))){
// filter unnecessary file/dir paths:
if($file!="." && $file!=".." && $file!="Thumbs.db"){

// put thumbnail path in array:
$pre_thumbs[$i]=$thumb_prefix.$file;

// put directory name in array - filter file extension:
$cut_num=strlen($file)-4;
$pre_dir_names[$i]=substr($file,0,$cut_num);

$i++;
}
}
closedir($handle);
}

Can anyone tell me how to order them in the array by time correctly (and reliably)?

coopster

9:23 pm on Jan 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can use filemtime [php.net] to get the timestamp and then sort your array on that value.

bramcorleone

10:05 pm on Jan 16, 2008 (gmt 0)

10+ Year Member



I know I have to use filectime, but I don't know how exactly.. Any thoughts?

coopster

5:03 am on Jan 17, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use filemtime, not filectime. Some more info here ...
Need to know about fileatime, filectime and filemtime [webmasterworld.com]

Basically, you want to loop through your directory and create a multidimensional array with secondary indexes such as filename, date, and size perhaps, etc. So, as you loop through you will load your array accordingly. Modify your array-building process within your loop:

$pre_dir_names[$i]['name'] = substr($file,0,$cut_num); 
$pre_dir_names[$i]['date'] = filemtime(/path/to/$file);

When you are done filling it, you can use the array sorting functions to sort as needed.

bramcorleone

3:21 pm on Jan 17, 2008 (gmt 0)

10+ Year Member



Thanks for pointing me the way.. it doesn't matter if I use filemtime or filectime (results are the same)..

I did test what the results were, when putting a file in a directory, when I was making a date added function and at that time filectime was the one I needed (gives the time of uploading).

The script above worked well on Windows, when deleting an old file and adding a new file. On my host (runs on freeBSD) it placed the new file at the place, where the old file stood before deletion.

Now the script works on both operating systems as is desired:)!

orbface9

5:12 am on Mar 15, 2008 (gmt 0)

10+ Year Member



Hello,

I have been trying to do something similar.

I am stuck at the point where i don't know if my values are getting posted to my array. Can someone take a look and let me know what the problem is?

function populatearray($dirname="images/"){
$pattern="(\.jpg$)¦(\.png$)¦(\.jpeg$)¦(\.gif$)";//Valid image extensions
$files = array();
$curimage=0;
if ($handle = opendir($dirname)) {
while (false !== ($file = readdir($handle))) {
if (eregi($pattern, $file)){
$files['name'][$curimage] = $dirname.$file;
$files['date'][$curimage] = filectime($dirname.$file);
}
}
closedir($handle);
}
return($files);
}

populatearray();

for (($i = 0); ($i < count($files['name'])); $i++) {
echo $files['name'][$i]." - ".$files['date'][$i]."\n";
}

I can't get the code to echo out the values of the array...

Any help would be appreciated.

Thanks

coopster

1:07 pm on Mar 17, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to capture the return value from your function into an array before you can use it:
$files = populatearray();