When trying to display image that's adress doesn't exist you will see default browsers "no image" badge.
How to change it to default "no-image.png" placeholder?
echo "<img src='http://www.google.com/trolol.png'>"; //for example You can use the onerror attribute, example:
<img src="" onerror="this.src = 'no-image.png';" alt="" /> <?php $error = "onerror='this.src = \'http://userlogos.org/files/logos/pek/stackoverflow2.png\'';"; for ($i = 1; $i <= 10; $i++) { echo "<img src='http://services.runescape.com/m=itemdb_rs/3716_obj_sprite.gif?id=" . $i . "' alt='ID " . $i . "' title='ID " . $i . "'" . $error . "/>"; } ?>?<img src='http://www.google.com/trolol.png' onerror="this.src='http://jsfiddle.net/img/logo.png'"> sample http://jsfiddle.net/UPdZh/1/
The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image).
Syntax
<element onerror="SomeJavaScriptCode"> <?php $filename = 'http://www.google.com/trolol.png'; if (file_exists($filename)) { echo "<img src='".$filename."'>"; } else { echo "<img src='no-image.png'>"; } ?> <?php $i = 2; $filename = "http://services.runescape.com/m=itemdb_rs/3716_obj_sprite.gif?id=" . $i; if (file_exists($filename)) { echo "<img src=" . $filename . " />"; } else { echo "NO IMAGE"; } ?>Try this
<?php $filename = 'http://www.google.com/trolol.png'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> As you asked for a PHP solution:
if (file_exists($filename)) { echo "i_exist.jpg"; } else { echo "fallback.jpg"; } IMPORTANT: Works only for local files, not for remote ones! Thanks to Szymon for the comment!