Forum Moderators: coopster
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)?
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);
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:)!
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