Search code examples
phplinuxfile-uploadfile-exists

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


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.


Solution

  • Gee, this was a pretty tuff one. I always make sure when I copy anything from the web to first paste it into a normal texteditor in order to remove all strange/hidden characters, then copy the whatever again and paste it into my dev tool.

    As I mentioned somewhere in a comment, I did pwd and copied the text form my virtual server to my osx. But what I didn't think about/know was that strange/hidden characters could follow if I did that from my linux server, hence I didn't make sure to copy everything via texteditor.

    I remembered that I had problem quite some time ago when I copied from the web and figured this may be a similar kind of problem. I opened my script in an hex editor. And what did I find... '/var' looked lite this: '/var'. Removing the strange characters fixed the problem.

    Thank you all for your comments above. Hope those can help someone else and perhaps it has helped me without even knowing it (since I did a lot of things based on your comments).