5

I have a case where file_exists() is always returning false. My latest attempt was to just test to see if it would return true for $_SERVER["SCRIPT_FILENAME"] and then return the value of the path if it couldn't find the file which it does.

The path while not necessarily relevant to solving the problem is: /Users/joe/Workspace/720/app/webroot/index.php

I have obviously verified that the file is there, and am not even sure how it couldn't be there since php is serving it up.

I should mention this is on an install of OS X Snow Leopard running PHP 5.3.0.

Any ideas would be fantastic.

CODE SAMPLE:

if (!file_exists($_SERVER["SCRIPT_FILENAME"]))
    $errors[] = 'Cant find:'. $_SERVER["SCRIPT_FILENAME"];
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Joe
  • 51
  • 1
  • 3
  • 1
    Clearly the function should work, so a code sample is in order. – wallyk Nov 25 '09 at 20:03
  • Posting the actual code may be helpful. – Tim Lytle Nov 25 '09 at 20:03
  • What's the value of `getcwd()`? `$_SERVER["SCRIPT_FILENAME"]` may be in a different directory. – Frank Farmer Nov 25 '09 at 20:59
  • I have the same issue, PHP version 5.2.14; getcwd() returns '/home/me/public_html'; file_exists('/images/products/me.jpg') returns false; file_exists('/home/me/public_html/images/products/me.jpg') returns true; I don't know why it fails, as on a different page file_exists('/images/products/me.jpg') returns true, but making the path absolute _always_ works; i.e. Paul's solution works for me too. – parvus Aug 04 '12 at 10:55

7 Answers7

8

I just experienced this issue and have been stuck on it for the past couple hours. The answer is that you need to specify the ABSOULTE path in order for file_exists() to work. You can NOT use relative paths such as 'dir1/images/image.jpg' or '../../images/image1.jpg'. You need to specify '/rootdir/subdir/dir1/images/myimage.jpg'.

That's what worked for me anyway.

Paul
  • 81
  • 1
  • 1
  • This solved it for me. It's a bit difficult to find the absolute path on shared servers since it may not be the root of your FTP account, but it can be found through phpinfo(). – DACrosby Dec 02 '14 at 22:02
3

It's probably a file permission issue. Make sure the file you are testing for is accessible by _www user (which is the user used to run apache {httpd} on Mac OS X).

Maybe you can try testing for a file on /tmp with 777 as it permission bits.

Hope it helps.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • I chmodded the file in question to 777 with the same results. I'm only using SCRIPT_FILENAME variable here to assure everyone that the file does indeed exist and this isn't a pathing typo. – Joe Nov 25 '09 at 20:10
1

From the php manual 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.

That's only a guess, a code sample may make things clearer.

Another reason file_exists() may not be able to access the file (not safe mode related):

Note: The check is done using the real UID/GID instead of the effective one.

This script works fine on my linux box (it's pretty much the example you added):

<?php
  if (file_exists($_SERVER["SCRIPT_FILENAME"])){
    echo "Found File: ";
  } else {
    echo "No File: ";
  }

  echo $_SERVER["SCRIPT_FILENAME"];
?>
Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
  • safe_mode is deprecated, but I also verified that the flag is off in the php.ini. – Joe Nov 25 '09 at 20:07
  • It is deprecated, but that certainly doesn't mean it's not enabled in different configuration - just that it shouldn't be. Maybe a UID/GID issue? – Tim Lytle Nov 25 '09 at 20:10
  • What different configuration would it be enabled it? Snow Leopard got some ninja configs around somewhere? I was under the impression this is a php specific setting not apache or otherwise? – Joe Nov 25 '09 at 20:13
  • I just mean that the various installations of php mean you'll run into safe mode regardless of its depreciation. With a standard install on your own machine it isn't likely, but still good to check. – Tim Lytle Nov 25 '09 at 20:18
  • 1
    In other words: "deprecated" doesn't mean it no longer works. It means it may no longer work in future versions, so you should avoid it. – JW. Nov 25 '09 at 20:53
1

Also check the parent directory, and all of their parents, to make sure that everyone has execute access.

If you're running this under Apache (instead of on the command line), remember that it runs under the _www user and _www group on Snow Leopard. So that's the group that needs access.

JW.
  • 50,691
  • 36
  • 115
  • 143
0

Use file_exists($_SERVER['DOCUMENT_ROOT']."/path/to/file") this way php will give it the absolute path for you.

Ryan Smith
  • 704
  • 2
  • 6
  • 13
0

This may be of help:
http://bugs.php.net/bug.php?id=48535

Specifically:

I've set the setting fastcgi.impersonate in php.ini to 1 (like recommendet in the documentation). If I set it to 0 it works.

ekhaled
  • 2,930
  • 20
  • 24
-1

Safe mode?

From PHP file_exists documentation:

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.

PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
  • safe_mode is deprecated, but I also verified that the flag is off in the php.ini. – Joe Nov 25 '09 at 20:08