Reading directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Reading directories

Post by JellyFish »

Why doesn't this work:

Code: Select all

	function listDirs ($dir)
	{
		$dir = opendir($dir);
		while (false !== ($file = readdir($dir)))
		{
			
	  	$results .= "<li>$file";
	  	if (is_dir($file))
	  	{
		  	$results .= "<ul>".listDirs($file)."</ul>";
	  	}
	  	$results .= "</li>";
	  	
		}
		return $results;
	}
	
	
	
	
	$dirTree = "<ul id='dirTree'>";
	$dirTree .= listDirs("/home/");
	$dirTree .= "</ul>";
	
I then echo $dirTree but nothing displays, the list doesn't even show up in the source html.

What I'm trying to do is make an unordered list of directories. I want a new unordered list for every directory within a directory. It should results with a nice indented list of directories within directories within directories etc.

Please help me with this one I hope it's not too complicated.

Thanks for reading my post. :D
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Changed my function to:

Code: Select all

	function listDirs ($path)
	{
		$dir = opendir($path);
		while (false !== ($file = readdir($dir)))
		{
	  	if (is_dir($path.$file))
	  	{			
		  	$results .= "<li>$file";
			  $results .= "<ul>".listDirs($path.$file)."</ul>";
		  	$results .= "</li>";
	  	}
		}
		return $results;
	}
And now ONLY the directories are displayed except for the child directories(the directories within directories).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You haven't included the DIRECTORY_SEPARATOR constant.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Where should I include it?
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

Code: Select all

$path.DIRECTORY_SEPARATOR.$file
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I have:

Code: Select all

4:	function listDirs ($path)
5:	{
6:		$dir = opendir($path);
7:		while (false !== ($file = readdir($dir)))
8:		{
9:	  	if (is_dir($path.$file))
	  	 {			
		  	$results .= "<li>$file";
			  $results .= "<ul>".listDirs($path.DIRECTORY_SEPARATOR.$file)."</ul>";
		  	$results .= "</li>";
	  	}
		}
		return $results;
	}
	
	
	
	
	$dirTree = "<ul id='dirTree'>";
	$dirTree .= listDirs("/");
	$dirTree .= "</ul>";
And get the erros:
Warning: opendir(/home/content/t/r/a/tradingtresure/html//././././././././././././././././././././././././. in /home/content/t/r/a/tradingtresure/html/manager/files.php on line 6

Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/t/r/a/tradingtresure/html/manager/files.php on line 7

Warning: is_dir(): Stat failed for /home/content/t/r/a/tradingtresure/html//././././././././././././././././././././././././././././././././././././ in /home/content/t/r/a/tradingtresure/html/manager/files.php on line 9
The third error keeps repeating till I stop the page.

Does anyone have a good way of displaying a list of directories with php?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You aren't detecting the two special directories "." and ".." therefore you get stuck in an infinite loop on "." as it points to the current directory.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

you also need to change

Code: Select all

is_dir($path.$file)
to

Code: Select all

is_dir($path.DIRECTORY_SEPARATOR.$file)
along with where you already changed it
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This has been many times before on these boards. Have you searched here yet? Also, there is a function in php 5 called scandir() that you might be able to tap into (or at the very least there are comments on that page that will do exactly what you are trying to do).
Post Reply