Ok…
I have actually tried this one and it seems to work.
[php]
<?
// Will assign /Apache2/htdocs/ as the path unless one was passed as a
// path parameter in the URL
$path = !empty ($_GET['path']) ? $_GET['path'] : "/Apache2/htdocs/";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path
";
//running the while loop
while ($file = readdir($dir_handle)) {
//irrelivant line below
if($file!="." && $file!=".."){
if (is_dir($file)) {
echo "Directory -
$file";
} else {
echo "
$file";
}
}
}
//closing the directory
closedir($dir_handle);
?>
[/php]
There were a couple of minor mistakes that would have made the previous one not work (or not work well).
First is in the code I listed the function as isdir when it should have been is_dir.
Next on line to echo out the Directory, I had
[php]echo “<a href=’” . $_SERVER[‘PHP_SELF’] . “?path=$path/$file’>$file
”;
[/php] and I removed the / between $path and $file (near the end of the anchor) and moved it to the end of the anchor (for subsequent directory navigation) and added something to make it clear that the link was a directory, leaving with this:
[php]echo “Directory - <a href=’” . $_SERVER[‘PHP_SELF’] . “?path=$path$file/’>$file
”;
[/php]
Also please note that in a *nix environment things tend to be case sensitive so that /Apache2/htdocs/ is not the same as /apache2/htdocs/.
But as stated above I was able to get the above code to work on my server (Linux) without a problem.
As a final note, let me also say that this can leave a vulnerability for navigation that could potentially give someone access to files not in the web-root. Therefore you would need to place some mechanism to prevent users from navigating out of the structure that you intend to allow them access to.