Take the 2-minute tour ×
$var = "http://site.com/image.png";

if (file_exists($var))
    echo 'yes';
    echo 'no';

Why does this script always returns false?

share|improve this question
Because you're looking at a URL, not a physical path on your computer (or web server) –  Phill Feb 3 '11 at 8:14

4 Answers 4

up vote 10 down vote accepted

Because that's not a file, it's a URL. file_exists is intended to detect whether a file or directory exists, not a web page.

If you're on the server, you could possibly convert it using $_SERVER['DOCUMENT_ROOT'] or find another way to locate it on the file system.

share|improve this answer

If you want to check the presence of remote files (actually "resources") then use:

$i = get_headers($url);
$ok = strpos($i[0], "200");

And check the HTTP status code.

share|improve this answer

Then most probably your server does not allow outside connections from within PHP.

You can check this by opening a local file and see if that works. If it does, your code is correct.

Make sure allow_url_fopen is set to ON.

share|improve this answer
Never mind, I jumped to conclusions. @paxdiablo has the correct answer. –  berkes Feb 3 '11 at 8:17

The $var variable should describe a path on the system, not a URL. For example

$var = "/var/www/html/image.png";
share|improve this answer

Your Answer


