note 17366 added to ref.dir
| From: | Some at php2 dot chek dot com | Date: | Wed, 05 Dec 2001 23:22:04 +0000 |
| Subject: | note 17366 added to ref.dir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-21653@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);
--
http://www.php.net/manual/en/ref.dir.php
http://master.php.net/manage/user-notes.php?action=edit+17366
http://master.php.net/manage/user-notes.php?action=delete+17366
http://master.php.net/manage/user-notes.php?action=reject+17366