5

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.

Nicsoft
  • 3,644
  • 9
  • 41
  • 70

8 Answers8

10

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
Timothée Groleau
  • 1,940
  • 13
  • 16
  • I made sure the permission is apache:apache 777 for all subdirectories upp to and including /28 – Nicsoft Feb 09 '12 at 14:57
2

Clutching at straws here...

Try su - apache (or whatever the user your webserver runs as.. apache2, www-data etc..) and then traversing the directory.

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

FreudianSlip
  • 2,870
  • 25
  • 24
2

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).

Nicsoft
  • 3,644
  • 9
  • 41
  • 70
1

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

yen1k
  • 558
  • 3
  • 9
  • I updated my post with this information. All directories in /var/www/html/smic/upload/28/ has apache:apache 777 as permission. I changed that just now. No difference in behaviour. – Nicsoft Feb 09 '12 at 14:51
  • I'm not on Linux system with PHP at this moment. I'll check it at the evening. So i'm not sure, if this correct. try "./smic/upload/28/ul.txt" or "smic/upload/28/ul.txt" if the php script is in folder html. – yen1k Feb 09 '12 at 15:18
  • Thanks, using an hex editor I found strange characters which corrupted my file. – Nicsoft Feb 09 '12 at 15:53
  • so the file is damaged? another file you can check/open? – yen1k Feb 09 '12 at 16:21
  • 1
    I fixed the file (the php-script) by removing the strange characters in an hex editor. Then it worked. See my own answer somewhere here. – Nicsoft Feb 09 '12 at 16:29
1

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

Userbn
  • 342
  • 1
  • 2
  • Not working (I wrote it did, but I must have made a mistake because it doesn't work, I probably tested incorrect script, deleted that comment). – Nicsoft Feb 09 '12 at 14:43
  • use __DIR__ constant to get the current directory, after use the relative path to your resource – Userbn Feb 09 '12 at 15:31
  • Thanks, using an hex editor I found strange characters which corrupted my file. – Nicsoft Feb 09 '12 at 15:52
0

Perhaps, in a name of the file there is a whitespace symbol. It's important.

frops
  • 2,196
  • 4
  • 29
  • 42
0

From the php.net manual, "Upon failure, an E_WARNING is emitted." make sure error reporting is set to a level that will show warnings and try it.

Also, "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.". You're not running in php safe mode are you? phpinfo() should tell you.

Garry Welding
  • 3,599
  • 1
  • 29
  • 46
0

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

James
  • 3,765
  • 4
  • 48
  • 79
  • I included this line for that: getcwd() prints /var/www/html/smic/modules/core/ticket – Nicsoft Feb 09 '12 at 14:13
  • In that case, try `$file = "../../../upload/28/ul.txt";` – James Feb 09 '12 at 17:28
  • It's actually solved already, see my own answer. The file were corrupt and I had to fix it using an hex editor to remove strange characters. Thanks anyway! – Nicsoft Feb 09 '12 at 18:01