PHP:is_file() and file_exists() return different results on same file

谁说胖子不能爱 提交于 2019-12-23 16:02:35

问题


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.


回答1:


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




回答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.



来源:https://stackoverflow.com/questions/8461332/phpis-file-and-file-exists-return-different-results-on-same-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!