View Full Version : Show image if its available...

08-01-2010, 07:33 AM
I need a script that will allow me to show an image, but if that image doesn't exists, then show a default image. Here's my scenario, I have an upload script where the user uploads a picture, now I need a script that will show that picture if it actually exists (meaning it was uploaded), if it doesn't exist, then I need it to show a "default" picture.

So basically:

Image show "/path/to/Picture1.jpg", but if Picture1.jpg doesn't exists, then show "domain.com/default.jpg"

Sorry, I just dont really know how to explain this.. :)

08-01-2010, 08:52 AM
You can use php's file_exists function to check if the file exists and display default.jpg if it doesn't.

Other approach is to use .htaccess in your image path and substitute the 404 error page with your default.jpg.

08-01-2010, 09:28 AM
I did this for one of my projects:

if (is_file($WRCMS['doc_root'].'/cooks/avatars/'.$owner_id.'-avatar-thumb.png')) {
$avatar = '<img src="'.$WRCMS['root'].'/cooks/avatars/'.$owner_id.'-avatar-thumb.png" width=32 title="avatar" alt="avatar">&nbsp;';
else {
$avatar = '<img src="'.$WRCMS['root'].'/cooks/avatars/default_avatar.png" height=32 width=32 title="default avatar" alt="default avatar">&nbsp;&nbsp;';

08-02-2010, 03:56 AM
Thanks Guys