directory not being recognized with is_dir() or filetype()

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
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

directory not being recognized with is_dir() or filetype()

Post by s.dot »

Code: Select all

if($handle = opendir(getcwd().DIRECTORY_SEPARATOR.'themes'))
{
	while(false !== ($file = readdir($handle)))
	{
		echo $file.' - '.filetype($file).'<br />';
	}
}
This is outputting....

Code: Select all

. - dir
.. - dir

Warning: filetype() [function.filetype]: Lstat failed for crap in C:\Apache2\htdocs\test\class\test.class.php on line 36
crap - 

Warning: filetype() [function.filetype]: Lstat failed for default in C:\Apache2\htdocs\test\class\test.class.php on line 36
default -
It seems PHP is not recognizing that 'crap' and 'default' are directories. I thought I needed a full path to this, so I used getcwd().DIRECTORY_SEPARATOR.'themes'. Even using a relative path 'themes/' it still does this.

I am using php 5.1.2, apache 2, on windows.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Here is the entire function.. in case that helps.

Code: Select all

//gather available themes
function get_available_themes()
{
	if($handle = @opendir('themes'))
	{
		$themes = array();
		while(false !== ($file = readdir($handle)))
		{
			echo $file;
			if(is_dir($file) && ($file != '.' && $file != '..'))
			{
				$themes[] = $file;
			}
		}
		
		if(empty($themes))
		{
			return false;
		}
	} else
	{
		return false;
	}
	
	return $themes;
}
Since I have the echo in there, it echos that it's finding all the directories...

Code: Select all

.
..
crap
default
If I throw an is_dir() in there... it outputs this

Code: Select all

.
..
It won't recognize my 'crap' and 'default' directories. :cry:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

readdir only returns the filename, not including the path given to opendir(), i.e. 'default' not 'themes/default'.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Bah. I have worked with these functions so many times, I felt for sure this was a bug. I even reported it to php.net as a bug with my first bug report ever. I guess everytime i've used them in the past they have in the same directory. Now i feel like a bag of suck. :cry:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you might also be interested in
http://de2.php.net/glob wrote:array glob ( string pattern [, int flags] )
[...]
Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
[...]
GLOB_ONLYDIR - Return only directory entries which match the pattern
Post Reply