file_exists() returns false, but the file DOES exist
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";
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...}