note 17366 deleted from ref.dir by jimw
| From: | jimw@php.net | Date: | Thu, 20 Dec 2001 21:27:47 +0000 |
| Subject: | note 17366 deleted from ref.dir by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-23448@lists.php.net to get a copy of this message | ||
This will create a object containing the directory and file structure from a set $baseDir position.
The function to display the tree is also included:
class siteTree
{
var $dirContents = array();
function siteTree ($currentPath)
{
$this->readDir ($currentPath);
return true;
}
function readDir ($currentPath)
{
$handle = opendir($currentPath);
while ($file = readdir($handle))
{
if ($file == "." || $file == "..")
continue;
if (is_dir($currentPath . $file))
{
$this->foundDir ($currentPath, $file);
}
else
{
$this->foundFile ($file);
}
}
}
function foundDir ($currentPath, $dirName)
{
$childDir = new siteTree ($currentPath . $dirName . "/");
$this->dirContents[$dirName] = $childDir;
return true;
}
function foundFile ($fileName)
{
array_push ($this->dirContents, $fileName);
return true;
}
}
function listSiteTree ($siteIndex, $level)
{
foreach ($siteIndex->dirContents as $dirName => $dirItem)
{
if (is_object ($dirItem))
{
$level++;
echo str_pad ("", $level, "-") .
"<b>$dirName</b><br>\n";
listSiteTree ($dirItem, $level);
$level--;
}
else
{
echo str_pad ($dirItem, $level + strlen($dirItem), "-", STR_PAD_LEFT) .
"<br>\n";
}
}
return true;
}
# Generates a directory tree structure containing files and directories
$siteIndex = new siteTree ($baseDir);
listSiteTree ($siteIndex, 0);