php file_exists is returning false even if file exist on my linux

Nicsoft picture Nicsoft · Feb 9, 2012 · Viewed 14.4k times · Source

This question has been asked many times, but none of the answers I found helped me.

I am trying to get php file_exists() to work. The only scenario when it works is when the php-file is in the same directory as the file to use file_exist() on and only using the file name (i.e. excluding the path). But it's not consequent behaviour, please see below.

Som information:

  • safe_mode=off
  • no symlinks for directory 28
  • no whitespace in the file name
  • All directories in /var/www/html/smic/upload/28/ has apache:apache 777 as permission.
  • Using php 5.3

PHP:

echo getcwd()
clearstatcache();
$file = 'file:///var/www/html/smic/upload/28/ul.txt';
//Also tried like this
//$file = '/var/www/html/smic/upload/28/ul.txt';

if(file_exists($file)){
    echo $file." exists";
}

getcwd() prints /var/www/html/smic/modules/core/ticket

The permission of the php-script and the file to be checked is apache:apache 777.

Some details about the directory structure:

[root@localhost 28]# pwd
/var/www/html/smic/upload/28

[root@localhost 28]# ls -l ul.txt
-rw-r--r--. 1 apache apache 2 Feb  9 10:50 ul.txt

[root@localhost 28]# chmod 777 ul.txt

[root@localhost 28]# ls -l ul.txt
-rwxrwxrwx. 1 apache apache 2 Feb  9 10:50 ul.txt

The behaviour didn't change after changing the permission of the file. The directory /28 has drwxr-xr-x. for apache user and apache group

For test, I also moved the actual php-script to /28, gave it apache:apache 777 rigths. Changed the $file to "ul.txt" (i.e. $file = 'ul.txt';). That works, the ul.txt file is found.

getcwd() prints then /var/www/html/smic/upload/28

As another test I tried to find another file excluding the path in the "ticket" directory, the file wasn't recognized.

I'm banging my head...

Any advice is appreciated.

Answer

Timothée Groleau picture Timothée Groleau · Feb 9, 2012

The permission of the file alone is not sufficient for php to assess file_exists. the process that runs php also needs permission to traverse all the parent directories of that file to get to it.

Check them one by one and verify that php can read and enter (r-x)

ls -ald /var
ls -ald /var/www
ls -ald /var/www/html
ls -ald /var/www/html/smic
ls -ald /var/www/html/smic/upload
ls -ald /var/www/html/smic/upload/28