Re: sorting files in directory
| From: | Justin French | Date: | Wed, 27 Nov 2002 14:19:51 +0000 |
| Subject: | Re: sorting files in directory | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-126280@lists.php.net to get a copy of this message | ||
on 28/11/02 12:01 AM, Nick Wilson (nick@tioka.com) wrote:
> I have several directories filled with files written like this:
>
> * 02-09-19-filename.etc
> * 02-10-02-anotherfile.whatever
>
> How does php order these files if read from the directory and printed to
> the screen? -- I need them in date order, do I need to sort them
> somehow?
I think PHP reads them in date created order, or something else... anyhoo,
what you need to do is read the filenames into an array, then sort the
array, either top-to-bottom or bottom-to-top:
<?
function files_array_sorted($start_dir, $reverse = 0)
{
chdir($start_dir);
$dir = opendir('.');
while (($myfile = readdir($dir)) !==false)
{
if(is_file($myfile))
{
$files[] = $myfile;
}
}
closedir($dir);
chdir('../');
if($reverse)
{ rsort($files); }
else
{ asort($files); }
reset($files);
return $files;
}
foreach(files_array_sorted('dummyfiles',1) as $filename)
{
echo "{$filename}<br />\n";
}
?>
There's an optional second parameter for asort() and rsort(), so check it
out in the manual... it *may* be needed to get what you want.
Justin French
--------------------
http://Indent.com.au
Web Development &
Graphic Design
--------------------