3

I am having an issue where file_exists returns false and is_file returns true.

echo(getmygid()." = gid\n"); //501
echo(getmyuid()." = uid\n"); //501
echo(posix_getgid()." = pgid\n"); //501
echo(posix_getuid()." = puid\n"); //501
var_dump(file_exists("/home/www/public_html/")); //bool(true)
var_dump(file_exists("/home/www/public_html/index.html")); //bool(false)
var_dump(is_file("/home/www/public_html/index.html")); //bool(true)

var_dump(stat("/home/www/public_html/index.php")); 

The output is:

501 = gid
501 = uid
501 = pgid
501 = puid
bool(true)
bool(false)
bool(true)
array(26) {
  [0]=>
  int(51712)
  [1]=>
  int(58055)
  [2]=>
  int(33197)
  [3]=>
  int(1)
  [4]=>
  int(501)
  [5]=>
  int(501)
  [6]=>
  int(0)
  [7]=>
  int(473)
  [8]=>
  int(1323573973)
  [9]=>
  int(1323573973)
  [10]=>
  int(1323574039)
  [11]=>
  int(4096)
  [12]=>
  int(8)
  ["dev"]=>
  int(51712)
  ["ino"]=>
  int(58055)
  ["mode"]=>
  int(33197)
  ["nlink"]=>
  int(1)
  ["uid"]=>
  int(501)
  ["gid"]=>
  int(501)
  ["rdev"]=>
  int(0)
  ["size"]=>
  int(473)
  ["atime"]=>
  int(1323573973)
  ["mtime"]=>
  int(1323573973)
  ["ctime"]=>
  int(1323574039)
  ["blksize"]=>
  int(4096)
  ["blocks"]=>
  int(8)
}

I imagine I have done something wrong in the configuration, but haven't quite figured out what it is.

What is even more exciting is that despite file_exists not working fread(fopen('/home/www/public_html/index.html','r'), filesize('/home/www/public_html/index.html')) does return the contents of the file.

teotwawki
  • 65
  • 1
  • 7
  • 2
    you have posted the stat on wrong file, can you correct that? – ajreal Dec 11 '11 at 02:32
  • All those directories (home, www, public_html) have read permission enabled for this script? And are they all directories, not symlinks? – Dalibor Filus Dec 11 '11 at 03:34
  • yeah. My bad. just fixed that. this is with it empty. and a 655 mode. – teotwawki Dec 11 '11 at 03:35
  • no symlinks. home directory is 766 www is drwxrwxr-x public_html is drw-r-xr-x – teotwawki Dec 11 '11 at 03:47
  • The stat is for index.php, is it suppose to be php or html? – Jim Dec 11 '11 at 05:17
  • Ah of course. Unfortunately it makes little difference whether it is php or html or even one more directory level. However, in the interest of giving the most complete information I will change this information shortly. – teotwawki Dec 12 '11 at 23:04
  • Brad, I finally got it! I'm not exactly sure that I fully understand why this is, but hopefully you can help. The public_html directory had permissions drw-r-xr-x, but the owner required x permissions to allow file_exists to work on its contents. So now that the public_html directory has drwxr-xr-x everything works properly. I still have a remaining question that maybe you can assist with: The user 'www' was also part of the group 'www' and the file's owner and group were also 'www', so why with those permissions would the owner not be able to view the directory contents? – teotwawki Jan 10 '12 at 22:16
  • dbuell: don't try to edit someone else's answer! Give them a comment, sure, but you shouldn't edit it. – Hovercraft Full Of Eels Jan 10 '12 at 23:14

2 Answers2

2

See the warning on file_exists():

This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

The is_file() function doesn't seem to have this restriction.

mauris
  • 42,982
  • 15
  • 99
  • 131
Brigand
  • 84,529
  • 20
  • 165
  • 173
2

Weird, here are a few options to check from the manual:

Note: The results of this function are cached. See clearstatcache() for more details.

Or this maybe:

Warning

This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

Those are the only things I can think of that might be effecting it. Not sure if you tried that or not, but worth a shot.

UPDATE

How about the file flags? From the shell (if you have shell access) can you do an ls -alh /home/www/public_html | grep index.html and make sure that a flag isn't set weird on it?

UPDATE 2

The problem is that the directory permissions were set so the owner could not view the directory contents. It is explained further in the comments

Community
  • 1
  • 1
Jim
  • 18,673
  • 5
  • 49
  • 65
  • According to phpinfo() safe_mode Off Off clearstatcache() did not change the results. – teotwawki Dec 11 '11 at 01:49
  • Correct, it is not a symlink. – teotwawki Dec 11 '11 at 01:54
  • How about the file flags? From the shell (if you have shell access) can you do an `ls -alh /home/www/public_html | grep index.html` and make sure that a flag isn't set weird on it? – Jim Dec 11 '11 at 02:13
  • I've tried it with 83886 -rwxrwxrwx 1 www www 2.6K etc... which is 777, but I've also tried 766 and 655 – teotwawki Dec 11 '11 at 02:53
  • (Copy from above)Brad, I finally got it! I'm not exactly sure that I fully understand why this is, but hopefully you can help. The public_html directory had permissions drw-r-xr-x, but the owner required x permissions to allow file_exists to work on its contents. So now that the public_html directory has drwxr-xr-x everything works properly. I still have a remaining question that maybe you can assist with: The user 'www' was also part of the group 'www' and the file's owner and group were also 'www', so why with those permissions would the owner not be able to view the directory contents? – teotwawki Jan 10 '12 at 22:17
  • That does explain it. The reason is, which I am not file expert, is that you cannot open a directory that is not at least 0755. If you chmod a directory to 0644, you will get a permission denied. So yea, that does make sense, I think root users can access it, just not anyone else. – Jim Jan 11 '12 at 06:24