Page 1 of 1

Directory Browsing!

Posted: Wed May 19, 2004 6:48 am
by GirishR
Hello Guys,

Please try this piece of code

Code: Select all

<?php
if (!$folder)
{
	$folder = ".";
}
chdir($folder);
print "<BR><BR>Current Folder -> $folder<BR>";
//print gettype($folder);
getDirList($folder);
function getDirList($dirName)
{
	$d = dir($dirName);
	print $dirName."<BR>";
	while($entry == $d->read())
	{
		if ($entry != "." && $entry != "..")
		{
			if (!is_file($entry))
			{
				//print $entry;
				print "<a href=directory_browser1.php3?folder=".htmlentities(urlencode($dirName))."/".htmlentities(urlencode($entry))."><font size=2 color=blue face=Tahoma>$entry</font></a><BR>";
				
				//getDirList($dirName."/".$entry);
			}
			else
			{
				//echo $dirName."/".$entry."\n";
			}
		}
	}
	//$d->close();
}
?>
This gives me werid error. Logicaly, I think every thing is correct. Can any one on of you fix it. please.

Regards
Girish R

?>[/php_man]

Posted: Wed May 19, 2004 6:59 am
by patrikG
What's the error you're getting?

Btw: I'm using this function which works quite nice:

Code: Select all

<?php
function get_directory_contents($path){
	$mydirectory = opendir($path);
	while($entryname=readdir($mydirectory)){
		switch (true){
			case ($entryname==".")               : break;
			case ($entryname=="..")              : break;
			case (is_dir($path."/".$entryname))  : $return["dir"][]=$entryname; break;
			default                              : $return["file"][]=$entryname;
		}
	}
	closedir($mydirectory);
	return $return;
}

echo "<h3>directory contents</h3><pre>";
print_r(get_directory_contents("./"));
echo"</pre>";
?>

Posted: Wed May 19, 2004 7:15 am
by GirishR
Ok patrikG, thats for script is for displaying th contents of a folder. But what I have coded is for browsing the directory by clicking each of the directory. It works fine for the first time but when clicked it gives a fatal error.

Thanks
Girish R

Posted: Wed May 19, 2004 7:47 am
by patrikG
What fatal error are you getting?

Also, change

Code: Select all

while($entry == $d->read())
to

Code: Select all

while($entry = $d->read())

Posted: Wed May 19, 2004 7:55 am
by GirishR
Here is the error

Warning: OpenDir: Invalid argument (errno 22) in c:\apache\htdocs\girishr\directory_browser1.php on line 12
./FCKeditor_1.6

Fatal error: Call to a member function on a non-object in c:\apache\htdocs\girishr\directory_browser1.php on line 14

Yes i did change the while($entry = $d->read()) to while($entry == $d->read())

Thanks

Posted: Wed May 19, 2004 8:06 am
by patrikG
I've changed $folder to $_GET["folder"] (see http://www.devnetwork.net/forums/viewtopic.php?t=511 ) and added a if (is_dir($dir)) to your function.

Code: Select all

<?php

if (!$_GET["folder"])
{
   $_GET["folder"] = ".";
}
print "<BR><BR>Current Folder -> $folder<BR>";

getDirList($_GET["folder"]);

function getDirList($dirName)
{
	if (is_dir($dirName)){
	   $d = dir($dirName);
	   print $dirName."<BR>";
	   while($entry = $d->read())
	   {
	      if ($entry != "." && $entry != "..")
	      {
	         if (!is_file($entry))
	         {
	            //print $entry;
	            print "<a href=temp1.php?folder=".htmlentities(urlencode($dirName))."/".htmlentities(urlencode($entry))."><font size=2 color=blue face=Tahoma>$entry</font></a><BR>";
	            
	            //getDirList($dirName."/".$entry);
	         }
	         else
	         {
	            //echo $dirName."/".$entry."\n";
	         }
	      }
	   }
   //$d->close();
  }
}

?>

Posted: Wed May 19, 2004 8:12 am
by GirishR
i got a 500 Internal Server Error.

Posted: Wed May 19, 2004 8:15 am
by GirishR
Sorry, my mistake did not change the file name. BTW what was the problem. A global variable?

Posted: Wed May 19, 2004 8:16 am
by patrikG
That's a server-issue - restart your server.

Posted: Wed May 19, 2004 8:17 am
by patrikG
The problem was that you tried to retrieve directory-contents of files and thus got error-messages back. The [php_man]is_dir[/php_man] if-clause I've added to your function prevents that.

Posted: Wed May 19, 2004 8:22 am
by GirishR
Thank you friend. That was very help full. BTW I am doing a search engine in php. So I wanted to add this feature fo users to add directories by selecting them rather that typing them. You can see the search engine script in http://www.hotscripts.com/Detailed/33759.html.

Thanks
Girish R

Posted: Wed May 19, 2004 8:26 am
by patrikG
:)