note 42589 deleted from function.readdir by derick
| From: | derick@php.net | Date: | Sat, 22 May 2004 16:04:29 +0000 |
| Subject: | note 42589 deleted from function.readdir by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69965@lists.php.net to get a copy of this message | ||
Note Submitter: paulg@i (hyphen) labs.com
----
Simplified recursive directory listing. Returns FolderListing object which contains an array with
all the directories and files.
<?php
class FolderListing {
var $newarr = array();
function loop($stack) {
if(count($stack) > 0) {
$arr = array();
foreach($stack as $key => $value) {
array_push($this->newarr, $stack[$key]);
if ($dir = @opendir($stack[$key])) {
while (($file = readdir($dir)) !== false) {
if (($file != '.') && ($file != '..')) {
array_push($arr, $stack[$key].'/'.$file);
}
}
}
}
$this->loop($arr);
} else {
$sorted = sort($this->newarr);
return($sorted);
}
}
}
$start = new FolderListing;
$base = array("baseDir");
$start->loop($base);
foreach($start->newarr as $value) {
echo $value."<br />";
}
?>
Add the following code around the array_push line to just check for directories:
if(is_dir($stack[$key].'/'.$file)) {
}