Help with script


#1

I have used a script which shows whats in the directory. But I want to change this so when I click on one of the folders(links) it opens up and shows a directory of that folder. The script is:

<?

$path = "/Apache2/htdocs/";


//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Directory Listing of $path<br/>";

//running the while loop

while ($file = readdir($dir_handle)) {
//irrelivant line below
   if($file!="." && $file!=".."){
 	echo "<a href='$file'>$file</a><br/>";

  }
}


//closing the directory
closedir($dir_handle);

?> 

I am stuck about what to do next. If someone could give me a pointer in the right direction that would be great.

Thanks,


#2

When the directory listing returns an entry that is a DIRECTORY ( http://php.net/isdir ) then you make the anchor appropriate so that it passes it to perhaps itself. From there you can “Update” the PATH variable to now do a dir in the new folder.

[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 (isdir($file) { echo "$file
"; } else { echo "$file
"; } } } //closing the directory closedir($dir_handle); ?>

[/php]

You might also consider maybe adding an image or some other information to denote that a FOLDER is a folder and not just some regular file.


#3

Hmm… I tried what you put but I get an error (blank screen). I am very new to PHP to try and debug it. Could you see what’s wrong please?

EDIT: I have seemed to find the line which is wrong:[php]
if (isdir($file) {
[/php]

I have delete the line the and it works(won’t know different between file and folder) but I don’t have an idea how to change it/edit it.


#4

Indeed that line is WRONG (sorry about that), but deleting it won’t solve the problem because that line is the line that determines if the $file being looked at is a DIRECTORY or not. When I first wrote it, I forgot to close the parenthesis on the IF condition.

Should be
[php]if (isdir($file)) {[/php]


#5

[DELETE]


#6

I have only just had time to see if it worked. And it doesn’t :cry:

This is the PHP:
[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 (isdir($file)) { echo "$file
"; } else { echo "$file
"; } } } //closing the directory closedir($dir_handle); ?>

[/php]

It only displays

Directory Listing of /Apache2/htdocs/

This is too beyond me to try and work it out. Could someone help?

thanks,


#7

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.


#8

EDIT: Still doesn’t work in the “if” it goes through the Else all the time.

[php]
if (is_dir($file)) {
echo “Directory - <a href=’” . $_SERVER[‘PHP_SELF’] . “?path=$path$file/’>$file
”;
} else {
echo “$file
”;
}
[/php]


#9

What is/are the following:

Operating System
Webserver
PHP Version


#10

Windows Xp Home Edition
Apache 2.2
PHP Version 5.2


#11

Directory handling in Windows is very different. For one, windows use the backslash instead of the forward slash / (like *nix systems).

So your Dirs should probably be listed in proper windows format. Additionally (and to complicate things) you must ESCAPE the backslash character because it IS the escape character. So you could list a dir as follows:

[php]$dir = ‘c:\apache2\htdocs’ [/php]

See if that will make a difference.


#12

Forget this post. It is working… To a decent standard.

Cheers.