note 24648 rejected from ref.dir by victor
| From: | victor@php.net | Date: | Sat, 24 Aug 2002 12:44:20 +0000 |
| Subject: | note 24648 rejected from ref.dir by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-35451@lists.php.net to get a copy of this message | ||
i wrote this function that recursively explores a directory and its subdirectories, and returns the
whole structure and all filenames as an XML tree.
function tree2xml ($dir, $level = 0)
{
if ($handle = opendir ($dir))
{
$tempFiles = array ();
$tempSubDirs = array ();
$space = str_repeat (' ', $level);
$xml = $space . "<dir name='" . $dir . "'>\n";
while (false !== ($file = readdir ($handle)))
{
if ($file != '.' && $file != '..')
{
if (is_dir ($dir . '/' . $file))
{
array_push ($tempSubDirs, $file);
}
else
{
array_push ($tempFiles, $file);
}
}
}
sort ($tempFiles);
sort ($tempSubDirs);
for ($i = 0; $i < count ($tempSubDirs); $i++)
{
$xml .= tree2xml ($dir . '/' . $tempSubDirs[$i], $level + 1);
}
for ($i = 0; $i < count ($tempFiles); $i++)
{
$xml .= $space . ' <file name=\'' . $tempFiles[$i] . "'
/>\n";
}
$xml .= $space . "</dir>\n";
closedir ($handle);
return $xml;
}
else return $dir;
}
// usage:
echo tree2xml ('.');