Jump to content

file_exists and is_file and is_dir - Unreliable?


Jesdisciple

Recommended Posts

I need to know whether any given filesystem node is a directory or a file, but file_exists and is_file and is_dir are sometimes all false when called on real nodes...? In a foreach-loop, I have this excerpt for debugging:

$file = .is_file: bool(false)is_dir: bool(true)found: bool(true)$file = ..is_file: bool(false)is_dir: bool(true)found: bool(true)$file = blogsis_file: bool(false)is_dir: bool(false)found: bool(false)$file = codeis_file: bool(false)is_dir: bool(false)found: bool(false)$file = css.phpis_file: bool(false)is_dir: bool(false)found: bool(false)$file = includesis_file: bool(false)is_dir: bool(false)found: bool(false)$file = index.phpis_file: bool(true)is_dir: bool(false)found: bool(true)$file = js.phpis_file: bool(false)is_dir: bool(false)found: bool(false)$file = me.phpis_file: bool(false)is_dir: bool(false)found: bool(false)$file = search.phpis_file: bool(false)is_dir: bool(false)found: bool(false)$file = site.phpis_file: bool(false)is_dir: bool(false)found: bool(false)$file = test.js.phpis_file: bool(false)is_dir: bool(false)found: bool(false)$file = title.phpis_file: bool(false)is_dir: bool(false)found: bool(false)

EDIT: And, although the directories have 62.4 GB of free space, the files are nowhere near 2 GB in size.Help?

Link to comment
Share on other sites

A directory may also contain a dot in its name, and thus appear to have an extension. Also, files may not have an extension. That is why is_file and is_dir may return different results.It's unclear what the base directory is. You might want to use realpath() just to make sure you're targeting the right place, i.e.:

echo '$file = ' . realpath($file) . PHP_EOL;

If even the absolute path appears to be the right one, check if you have reading permissions for that folder (though this shouldn't be the problem, otherwise even scandir() wouldn't have worked).[edit]Actually, if the file/folder doesn't exist, realpath() will return false. So... just analyze what "realpath('.')" (i.e. the first iteration of the loop) gives and make sure this is the directory in which you expect the files/folders to be.[/edit]

Link to comment
Share on other sites

My first PHP script did this stuff. I just dug it out and both those functions work fine. I suspect Robot's right: some path info is missing in your filename.EDIT: Like maybe the dir you input into scandir() is *not* the same dir as your script? That would make sense, since you got the correct results for . and .. and index.php, which simply means that those items exist in your base dir, because they exist in all dirs, but the other items are false because you're digging in the wrong place.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...