note 46082 deleted from function.readdir by nlopess
| From: | nlopess@php.net | Date: | Wed, 29 Sep 2004 14:33:13 +0000 |
| Subject: | note 46082 deleted from function.readdir by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77567@lists.php.net to get a copy of this message | ||
Note Submitter:
----
Yet another recursive directory lister :P
Why did I create it?
I like to keep things small, and it seems like all the scripts around here are a lot bigger and more
complex while they produce the same (or nearly the same) output.
This one creates a multidimensional array with the folders as associative array and the files as
values.
It puts the name of the folders and files in it, not the complete path (I find that annoying but its
easy to change that if you prefer it with the path).
And I used a for loop instead of a while loop like everyone around here does.
Why?
1. I just like for loops.
2. Everyone else uses a while loop.
<?php
function ls($dir){
$handle = opendir($dir);
for(;(false !== ($readdir = readdir($handle)));){
if($readdir != '.' && $readdir != '..'){
$path = $dir.'/'.$readdir;
if(is_dir($path)) $output[$readdir] = ls($path);
if(is_file($path)) $output[] = $readdir;
}
}
return isset($output)?$output:false;
closedir($handle);
}
print_r(ls('.'));
?>
PS: if you don't need the files in it then you can just comment out the
"if(is_file....." part.