note 46083 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:05:02 +0000 |
| Subject: | note 46083 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126536@lists.php.net to get a copy of this message | ||
Note Submitter: Rick at Webfanaat dot nl
----
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.