note 42589 added to function.readdir
| From: | paulg at i dot php dot net | Date: | Sat, 22 May 2004 14:04:29 +0000 |
| Subject: | note 42589 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69960@lists.php.net to get a copy of this message | ||
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)) {
}
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42589
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42589&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42589&report=yes
Search -- http://master.php.net/manage/user-notes.php