file_exists() returns false, but the file DOES exist

旧街凉风 提交于 2019-11-28 07:14:28

Results of the file_exists() are cached, so try using clearstatcache(). If that not helped, recheck names - they might be similar, but not same.

Mahdi Loghmani

file_exists() just doesn't work with HTTP addresses.

It only supports filesystem paths (and FTP, if you're using PHP5.)

please note:

worked :

if  (file_exists($_SERVER['DOCUMENT_ROOT']."/folder/test.txt") 
    echo "file exists";

not worked :

if (file_exists("www.mysite.com/folder/test.txt") 
    echo "file exists";
ace

It's because of safe mode. You can turn it off or include the directory in safe_mode_include_dir. Or change file ownership / permissions for those files.

php.net: file_exists()
php.net: safe mode

Shahar

I found that what works for me to check if a file exists (relative to the current php file it is being executed from) is this piece of code:

$filename = 'myfile.jpg';
$file_path_and_name = dirname(__FILE__) . DIRECTORY_SEPARATOR . "{$filename}";
if ( file_exists($file_path_and_name) ){
  // file exists. Do some magic...              
} else {
  // file does not exists...
}

Just my $.02: I just had this problem and it was due to a space at the end of the file name. It's not always a path problem - although that is the first thing I check - always. I could cut and paste the file name into a shell window using the ls -l command and of course that locates the file because the command line will ignore the space where as file_exists does not. Very frustrating indeed and nearly impossible to locate were it not for StackOverflow.

HINT: When outputting debug statements enclose values with delimiters () or [] and that will show a space pretty clearly. And always remember to trim your input.

Try using DIRECTORY_SEPARATOR instead of '/' as separator. Windows uses a different separator for file system paths (backslash) than Linux and Unix systems.

It can also be a permission problem on one of the parent folders or the file itself.

Try to open a session as the user running your webserver and cd into it. The folder must be accessible by this user and the file must be readable.

If not, php will return that the file doesn't exist.

A very simple trick is here that worked for me.

When I write following line, than it returns false.

if(file_exists('/my-dreams-files/'.$_GET['article'].'.html'))

And when I write with removing URL starting slash, then it returns true.

if(file_exists('my-dreams-files/'.$_GET['article'].'.html'))
scipilot

I have a new reason this happens - I am using PHP inside a Docker container with a mounted volume for the codebase which resides on my local host machine.

I was getting file_exists == FALSE (inside Composer autoload), but if I copied the filepath into terminal - it did exist! I tried the clearstatche(), checked safe-mode was OFF.

Then I remembered the Docker volume mapping: the absolute path on my local host machine certainly doesn't exist inside the Docker container - which is PHP's perspective on the world.

(I keep forgetting I'm using Docker, because I've made shell functions which wrap the docker run commands so nicely...)

have you tried manual entry. also your two extensions seem to be in different case

   var_dump(file_exists('../../images/example/001-001.jpg'));
   var_dump(file_exists('../../images/example/001-001.PNG'));

A custom_file_exists() function inspired by @Timur, @Brian, @Doug and @Shahar previous answers:

function custom_file_exists($file_path=''){
    $file_exists=false;

    //clear cached results
    //clearstatcache();

    //trim path
    $file_dir=trim(dirname($file_path));

    //normalize path separator
    $file_dir=str_replace('/',DIRECTORY_SEPARATOR,$file_dir).DIRECTORY_SEPARATOR;

    //trim file name
    $file_name=trim(basename($file_path));

    //rebuild path
    $file_path=$file_dir."{$file_name}";

    //If you simply want to check that some file (not directory) exists, 
    //and concerned about performance, try is_file() instead.
    //It seems like is_file() is almost 2x faster when a file exists 
    //and about the same when it doesn't.

    $file_exists=is_file($file_path);

    //$file_exists=file_exists($file_path);

    return $file_exists;
}

This answer may be a bit hacky, but its been working for me -

$file = 'path/to/file.jpg';
$file = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$file;
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}else{
    $exists = true;
}

apparently $_SERVER['REQUEST_SCHEME'] is a bit dicey to use with IIS 7.0 + PHP 5.3 so you could probably look for a better way to add in the protocol.

I found this answer here http://php.net/manual/en/function.file-exists.php#75064

Irshad Khan

I spent the last two hours wondering what was wrong with my if statement: file_exists($file) was returning false, however I could call include($file) with no problem.

It turns out that I didn't realize that the php include_path value I had set in the .htaccess file didn't carry over to file_exists, is_file, etc.

Thus:

<?PHP
// .htaccess php_value include_path '/home/user/public_html/';

// includes lies in /home/user/public_html/includes/

//doesn't work, file_exists returns false
if ( file_exists('includes/config.php') )
{
     include('includes/config.php');
}

//does work, file_exists returns true
if ( file_exists('/home/user/public_html/includes/config.php') )
{
    include('includes/config.php');
}
?>

Just goes to show that "shortcuts for simplicity" like setting the include_path in .htaccess can just cause more grief in the long run.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!