note 42590 modified in function.readdir by derick
| From: | derick@php.net | Date: | Sat, 22 May 2004 17:25:09 +0000 |
| Subject: | note 42590 modified in function.readdir by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69968@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);
}
}
}
closedir($dir);
}
$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)) {
}
--was--
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);
}
}
}
closedir($dir);
}
$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)) {
}
http://www.php.net/manual/en/function.readdir.php