note 104681 deleted from function.scandir by danbrown
| From: | danbrown@php.net | Date: | Thu, 30 Jun 2011 13:42:48 +0000 |
| Subject: | note 104681 deleted from function.scandir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181101@lists.php.net to get a copy of this message | ||
Note Submitter: brennersimon at hotmail dot com
----
Gerard, thanks for your code snippet. I've just been looking for something like that.
But I had to change some little things:
- Pay attention to a separating slash / backslash between $path and $file. If it's not there,
the output is wrong.
- The dirs were sorted reversely and the files where ok.
My solution was:
<?php
function scandirSorted ($path) {
$files = array();
$dirs = array();
foreach (scandir($path) as $file) {
if (is_file($path . "/" . $file)) { // Linux: "/", Windows:
"\\"
array_push($files, $file);
} else {
array_push($dirs, $file);
}
}
return array_merge($dirs, $files);
}
?>
So I'm filling up two different arrays. One with directories and one with files. In the end, I
return them as one single array.