problem with is_dir()

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
yarons
Forum Newbie
Posts: 24
Joined: Mon Sep 11, 2006 7:25 am

problem with is_dir()

Post by yarons »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,
I have this strange problem with the is_dir() function.
I have a piece of code that will loop through all files in a certain directory and will put only regular files (i.e. no dircetories) in a select drop down.
Here's my code:

Code: Select all

echo "<tr><td><select name='dFile'>";
		if ( $dDir = @openDir($rootDir) )
		{
		
			while ( ($dFile = @readDir($dDir)) !== FALSE )
			{
				if ( $dFile == "." || $dFile == ".." || is_dir($dFile) )
					continue;
				echo "<option value='$rootDir/$dFile'>$dFile";
			}//while
			closedir($dDir);
	
		}//if
		echo "</select></td></tr>";
My problem is that this code WILL display also directories. Not all of them, though, it will skip just one (I think it's the first one).
So in a directory where I have 2 files and 3 dirs, I get in the select box the 2 files and 2 dirs.
What am I missing here?

Many thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

readdir() returns only the file name not the complete path. So unless $rootDir is '. ' is_dir($dFile) will not work.
yarons
Forum Newbie
Posts: 24
Joined: Mon Sep 11, 2006 7:25 am

Post by yarons »

volka wrote:readdir() returns only the file name not the complete path. So unless $rootDir is '. ' is_dir($dFile) will not work.
That explains a lot!
I should have checked that ;)

Thanks very much.
Post Reply