note 60383 deleted from function.scandir by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Jan 2010 14:16:52 +0000 |
| Subject: | note 60383 deleted from function.scandir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165449@lists.php.net to get a copy of this message | ||
Note Submitter: carlfredrik dot bowin at gmail dot com
----
I made a function that scans the directory and all it subfolders. You can also sort them with
folders first and files after, in alphabetic order. It works with both PHP 4 and 5.
<?php
function rec_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false ) {
if ( $file != ".." && $file != "." ) {
if ( is_dir($dir . "/" . $file) ) {
$files[$file] = rec_scandir($dir . "/" . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
function cmp($a, $b)
{
if ( is_string($a) && is_string($b) ) {
return strcmp($a, $b) > 0 ? 1 : -1;
}elseif ( is_int($a) && is_int($b) ) {
return $a > $b ? 1 : -1;
}elseif ( is_int($a) && is_string($b) ) {
return 1;
}elseif ( is_string($a) && is_int($b) ) {
return -1;
}else {
return 0;
}
}
function array_ukmultisort(&$arr, $func) {
uksort($arr, $func);
while ( list($key, $val) = each($arr) ) {
if ( is_array($val) ) {
array_ukmultisort($arr[$key], $func);
}
}
}
$dir = rec_scandir(".");
array_ukmultisort($dir, "cmp");
echo "<pre>";
print_r($dir);
echo "</pre>";
?>