Hello,
I have a script which loads directory information into a multi-dimensional array and then displays the array information. The array consists of the filename, filesize and timestamp for each file. It is my understanding that when using readdir() to access files information that it reads/displays the information in the order in which the files were places on the server, but when I display the date and time information it appears to be displaying the information randomly.
Here is the code that loads the information into the multi-dimensional array:
[php]
//get file info
$thefiles=array();
$thesizes=array();
$thedates=array();
$counter=0;
while (false !== ($item = readdir($dp)))
{
if (substr($item,0,1)!=’.’) {
if (is_file($activedir.$item)) {
$counter=$counter+1;
$thefiles[$counter]=$item;
$thesizes[$counter]=filesize($activedir.$item);
$thedates[$counter]=filemtime($activedir.$item);
}
}
}
$filesarray=array(‘files’ => $thefiles, ‘sizes’ => $thesizes, ‘dates’ => $thedates);
[/php]
I then use a do…while script to display the information. i have removed all of the interspersed HTML code that is in my actual script for brevity’s sake:
[php]
$newcounter=0;
do {
$newcounter=$newcounter+1;
// print the counter number and the file name
print $newcounter." ".$filesarray['files'][$newcounter];
// format and print the date
$outdate=date(m,$filesarray['dates'][$newcounter]).'/'.date(d,$filesarray['dates'][$newcounter]).'/'.date(Y,$filesarray['dates'][$newcounter]).' '.date(h,$filesarray['dates'][$newcounter]).':'.date(i,$filesarray['dates'][$newcounter]);
print $outdate."<br />";
//print the file size
print $filesarray['sizes'][$newcounter]."<br />";
} while ($newcounter < $counter);
[/php]
The do…while should simply run through the array and display the information in the order in which it was read from the directory, which should be in the order in whch they were upload to the directory, as I understand it. However what I end up getting is the files listed in what seems to be a random order:
1 Umlaut01.jpg 04/23/2011 06:41 821.5 kb
2 icon_file.gif 04/23/2011 06:58 196.0 b
3 Umlaut02.jpg 04/23/2011 06:42 787.6 kb
4 title.gif 04/23/2011 06:58 1.3 kb
Can anyone tell me why the output is coming out the way it is?
Additionally, how can I sort the multi-dimensional array by the timestamp element?
Thanks!