Php file_exists returns false even if the file exists in 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 (), and only using the file name (i.e. excluding the path). But this is not consistent behavior, see below.

Information about Som:

  • safe_mode = off
  • no symbolic links for directory 28
  • no spaces in file name
  • All directories in / var / www / html / smic / upload / 28 / have 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 php script permission and the file to be checked are apache: apache 777.

Some directory structure information:

 [ 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 behavior did not change after changing the file resolution. The / 28 directory has drwxr-xr-x. for apache user and apache group

For the 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';). This works, the ul.txt file is found.

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

As another test, I tried to find another file, excluding the path in the "ticket" directory, this file was not recognized.

I hit my head ...

Any advice is appreciated.

+4
source share
8 answers

Yes, it was pretty tuff. I always check when I copy something from the Internet, to first paste it into a regular text editor, to remove all strange / hidden characters, and then copy everything again and paste into my dev tool.

As I mentioned somewhere in the comment, I made pwd and copied the text form of my virtual server into my OSX. But what I didn’t think / know about was that weird / hidden characters could follow if I did this from my Linux server, so I didn't have to copy everything through texteditor.

I remembered that I had a problem for a long time, when I copied from the Internet and realized that this could be a similar problem. I opened my script in a hex editor. And what I found ... '/ var' looked like this: 'ï "¿/ var'. Fixed the problem with deleting strange characters.

Thanks to everyone for your comments above. I hope they can help someone else, and maybe it helped me without even knowing it (since I did a lot of things based on your comments).

+2
source

File-only permissions are not enough for php to evaluate file_exists. the process that starts php also needs permission to traverse all the parent directories of this file in order to get to it.

Check them one by one and make sure php can read and type (rx)

 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 
+7
source

The grip of straws is here ...

Try su-apache (or whatever user runs your web server like apache2, www-data, etc.) and then move the directory.

Are there any .htaccess files or any other restrictions on your apache configuration?

+2
source

what about folder permissions? path $file = 'file:///var/www/html/smic/upload/28/ul.txt'; is incorrect. Try copying the PHP script to the upload folder and file='28/ul.txt'; . The folder must be readable.

+1
source

Try opening the file using $ file = '/upload/28/ul.txt';

+1
source

From the php.net manual: "E_WARNING is selected on failure." make sure the error report is set to a level that will show warnings and try.

In addition, "This function returns FALSE for files inaccessible due to safe mode restrictions. However, these files can still be included if they are in safe_mode_include_dir.". You are not working in safe mode in php? phpinfo () should tell you.

0
source

Where is your php file located? If in the folder "html" use a relative path, for example $file = "smic/upload/28/ul.txt";

0
source

There may be a space character in the file name. It is important.

0
source

Source: https://habr.com/ru/post/1395603/


All Articles