Page 1 of 1

Reading directories

Posted: Wed May 16, 2007 8:09 pm
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

Posted: Wed May 16, 2007 8:21 pm
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).

Posted: Wed May 16, 2007 8:27 pm
by feyd
You haven't included the DIRECTORY_SEPARATOR constant.

Posted: Wed May 16, 2007 9:17 pm
by JellyFish
Where should I include it?

Posted: Wed May 16, 2007 9:23 pm
by maliskoleather

Code: Select all

$path.DIRECTORY_SEPARATOR.$file

Posted: Thu May 17, 2007 2:58 pm
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?

Posted: Thu May 17, 2007 3:44 pm
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.

Posted: Thu May 17, 2007 4:45 pm
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

Posted: Thu May 17, 2007 6:04 pm
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).