file_exists() returns false, but the file DOES exist

后端 未结 14 1282
一向
一向 2020-12-05 09:40

I\'m having a very weird issue with file_exists(). I\'m using this function to check if 2 different files in the same folders do exist. I\'ve double-checked, they BOTH do ex

14条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 10:32

    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

提交回复
热议问题