Re: Random results with readdir()
| From: | Philip Olson | Date: | Thu, 28 Dec 2000 10:20:01 +0000 |
| Subject: | Re: Random results with readdir() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-32066@lists.php.net to get a copy of this message | ||
On Wed, 27 Dec 2000, Joe Nelson wrote:
> Is there any way to sort the results of readdir()? It seems to order them
> by the last accessed date and I'd like them in alphabetical order.
Hi Joe-
Your question sparked a function to be created below. It's a bit overkill
for this task but should/could/will be useful. It's also v.0.0.1 :-)
First, here are a few types of sorts you can use :
sort = lowest to highest
asort = alphabetical order
rsort = sort in reverse order
The best explanation of each is in the manual. With regard to the
function below try :
$file_list = getDirArray($dir='./','asort');
Which will be an asorted file list from current directory. One way to
print out the results of such a beast is :
foreach ($file_list as $file) {
print "<br>$file \n";
}
Or call it directly :
foreach (getDirArray($dir='./','asort') as $file) {
print "<br>$file \n";
}
Hopefully this will be of use. It also makes count available as
$dir_file_count. All thoughts welcome.
learning and listening,
Philip Olson
<?php
// Get file list and sort according to users definition
// $dir = file directory to be "arrayed"
// $sort = sort function to use
function getdirArray($dir='./',$sort='sort')
{
global $dir_file_count;
if ( is_dir($dir) ) {
$fd = @opendir($dir);
while ( ($part = @readdir($fd)) == TRUE ) {
clearstatcache();
if ($part != "." && $part != "..") {
$dir_array[] = $part;
}
}
if($fd == TRUE) {
closedir($fd);
}
if (is_array($dir_array)) {
$sort($dir_array);
$dir_file_count = count($dir_array);
Return $dir_array;
} else {
Return FALSE;
}
}
}// EOF;
?>