I'm trying to make a recursive directory listing script, I've seen another one at http://www.phpbuilder.com/snippet/detail.php?type=snippet&id=865 that does what I want but the coding looks a little bad. I've tried to clean it up and this is what I've got.
<?php
function SiteMap($SiteRoot) {
if ($OpenDir = opendir($SiteRoot)) {
while (false !== ($Item = readdir($OpenDir))) {
if (is_dir($Item) && $Item != "." && $Item != "..") {
echo "<b>$Item</b>\n";
echo "<blockquote>\n";
SiteMap($SiteRoot."/".$Item);
echo "</blockquote>\n";
}
elseif (is_file($Item)) {
echo "$Item<br>\n";
}
}
unset ($Item);
closedir($OpenDir);
}
unset ($OpenDir);
}
SiteMap($_SERVER['DOCUMENT_ROOT']);
?>
What happens is I get a listing of the first dir. The blockquotes also show up so I know that it's getting that far but when it comes to calling the function again it doesn't work. The other script (posted below) works just fine. I don't know what I'm doing wrong. I THINK my script is doing the same as below but just with a little cleaner code. Any help with this would be great!
Thanks,
Ryan
<?
// Copyright 2002 Adam Taylor
// Email [email]adam@mpp.com.au[/email]
function ListFiles($root) {
if (!$root) $root = $_SERVER["DOCUMENT_ROOT"]."/"; // No brackets, I know this will be set, no need for it in my script
if ($dir = @opendir($root)) {
echo "<B>$root</b><BR\n>"; // I don't need this, took it out on my script
$file = readdir($dir); // This is set later...
$file = readdir($dir); // Ah, ya...
$num = ""; // varify folder or dir later (I used is_dir and is_file instead)
while (($file = readdir($dir)) !== false) { // A little backwards but it still works
$num = strstr($file, '.') ; // Sets the extention or if there is a . in $file
if ($num != "") { // If there was a . in $file...
echo "$file<BR>\n";
} else { // If there is no . in $file must be folder (ya, I'd hope so)
$newroot = $root.$file; // Sets the new docroot
echo "<BLOCKQUOTE>\n";
ListFiles($newroot."/"); // Calles the function again with new doc root
echo "</BLOCKQUOTE>\n";
}
}
closedir($dir);
}
}
// Syntax: ListFiles( Root Directory);
//
// The following will show all files in the document folder
// of your server.
ListFiles($_SERVER["DOCUMENT_ROOT"]."/");
?>