tags:

views:

1121

answers:

4

Has anyone encountered this oddity?

I'm checking for the existence of a number of directories in one of my unit tests. is_dir is reporting true (1) in spite of the folder not existing at the time it is called. Code looks like this (with a few extraneous intermediate vars to ease debugging):

foreach($userfolders as $uf) {
    $uf = sprintf($uf, $user_id);
    $uf = ltrim($uf,'/');
    $path = trim($base . '/' . $uf);

    $res = is_dir($path); //returns false except last time returns 1
    $this->assertFalse($res, $path);
}

Machine running Ubuntu Linux 8.04 with PHP Version 5.2.4-2ubuntu5.3

Things I have checked:

  • Paths are full paths
  • Same thing happens on two separate machines (both running Ubuntu)
  • I have stepped through line by line in a debugger
  • Paths genuinely don't exist at the point where is_dir is called
  • While the code is paused on this line, I can actually drop to a shell and run the interactive php interpreter and get the correct result
  • The paths are all WELL under 256 chars
  • I can't imagine a permissions problem as the folder doesn't exist! The parent folder can't be causing permissions problems as the other folders in the loop are correctly reported as missing.

Comments on the PHP docs point to the odd issue with is_dir but not this particular one.

I'm not posting this as a "please help me fix" but in the hope that somebody encountering the same thing can search here and hopefully an answer from somebody else who as seen this!

A: 

For what its worth, is_readable can be used as a work around.

reefnet_alex
+2  A: 

I don't think this would cause your problem, but $path does have the trailing slash, correct?

Thomas Owens
A: 
$path = trim($base . '/' . $uf);

That could be causing it. I'm assuming $base is some sort of root folder you are searching, so if $uf is something like '', '.', or '../' that could return true. We would have to see what values you are using in your foreach to know anything further.

[EDIT]

Doing some more looking the above code works fine on OpenBSD 4.3 with PHP 5.2.

dragonmantank
thanks for taking the time. I had not seen any unexpected values in my path, and I suspect the issue may only crop up with certain names. In this case the problem was with the non-existence of a folder ending in /profile Surprisingly Mr Owens suggestion was bang on the button!
reefnet_alex
A: 

@Thomas Owens

Amazing. No my paths didn't have a trailing slash. I wouldn't have expected them to need a trailing slash as:

  • I never habitually added trailing slashes before.
  • All other uses of is_dir in the same set of tests were working OK (though interestingly one other use failed in exactly the same way with the same non-existent folder name).
  • And the is_dir code examples in the php manual don't use a trailing slash either.
  • Surely, couldn't require a trailing slash. That what almost defeat the point of an is_dir test?!

But do you know what Mr Owens? You're absolutely right. Further tests reveal that adding trailing slashes to all the paths (none had them before) causes is_dir to behave.

Very strange behavior. And once again SO proves itself invaluable. So, in summary:

If you are having strange problems with is_dir in PHP, check that your paths end with a trailing slash or switch to is_readable instead!

reefnet_alex