file_exists() returns false, but the file DOES exist

Bv202 picture Bv202 · Aug 3, 2011 · Viewed 63.6k times · Source

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

echo $relative . $url['path'] . '/' . $path['filename'] . '.jpg';
Result: ../../images/example/001-001.jpg

echo $relative . $url['path'] . '/' . $path['filename'] . '.' . $path['extension'];
Result: ../../images/example/001-001.PNG

Now let's use file_exists() on these:

var_dump(file_exists($relative . $url['path'] . '/' . $path['filename'] . '.jpg'));
Result: bool(false)

var_dump(file_exists($relative . $url['path'] . '/' . $path['filename'] . '.' . $path['extension']));
Result: bool(true)

I don't get it - both of these files do exist. I'm running Windows, so it's not related to a case-sensitive issue. Safe Mode is off.

What might be worth mentioning though is that the .png one is uploaded by a user via FTP, while the .jpg one is created using a script. But as far as I know, that shouldn't make a difference.

Any tips?

Thanks

Answer

Mahdi Loghmani picture Mahdi Loghmani · Mar 10, 2017

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

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

Please note:

Works :

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

Does not work:

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