directory listing help / filetype() error


#1

Hello.

I’m working on a minimalist “photo blog” site. My goal right now is to make a dynamic menu that lists certain folders in my home directory. i.e. as soon as I create a new folder (via ftp), it shows up in the menu included at the bottom of every page.

Here’s what I can do right now: On my index.php page, I can get it to list the contents of the directory, showing only certain folders, like so:

[code]<?

	$path = "/home/content/d/r/o/drolldurham/html";
	
	$dir_contents = opendir($path);
	
	echo "directory listing of $path<br/><br/>";
	
	while ($file = readdir($dir_contents)) 
	{
		$filetype = filetype($file);
		
		if($filetype=="dir" && $file!="stats" && $file!="common" && $file!="." && $file!="..") {
			echo "<a href='$file'>$file</a> -- $filetype<br />";
		}
	}
	
	//closing the directory
	closedir($dir_contents);
	
	?> [/code]

This works perfectly. Now, one of my directories is called “europe”, and as a test I copied that exact same code to europe/index.php. For some reason things get messy with the $filetype line. If I omit the filetype code (along with “$filetype==‘dir’”) it does what it should do: list the contents of the root directory (not the contents of /europe). So why does everything work fine except for $filetype = filetype($file) ?

Is there a better way to be doing this?

The error message:

Warning: filetype(): Lstat failed for (null) (errno=2 - No such file or directory) in /home/content/d/r/o/drolldurham/html/common/menu.inc on line 25

Thanks!


#2

readdir() - The manual perfectly explains what (may) go wrong:

[php]/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo “$filen”;
}[/php]