Joe
Joe

Reputation: 51

PHP File Exists Always False

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"];

Upvotes: 5

Views: 6938

Answers (8)

Scott C Wilson
Scott C Wilson

Reputation: 20016

On a Mac, the cd command is case insensitive, but the PHP file_exists function is not! So if you had a folder named (say) sites, then

cd Sites works but

file_exists('/home/userid/Sites/mysite/config.php') will return FALSE.

Upvotes: 0

Ryan Smith
Ryan Smith

Reputation: 694

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

Upvotes: 0

Paul
Paul

Reputation: 81

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.

Upvotes: 8

JW.
JW.

Reputation: 51638

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.

Upvotes: 1

ekhaled
ekhaled

Reputation: 2930

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.

Upvotes: 0

Tim Lytle
Tim Lytle

Reputation: 17624

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"];
?>

Upvotes: 1

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

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.

Upvotes: -1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

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.

Upvotes: 3

Related Questions