display a default image?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
direland
Forum Newbie
Posts: 2
Joined: Wed May 31, 2006 7:24 pm

display a default image?

Post by direland »

Is there an "if" statement I can use to check if an image exists in a certain directory, and if it does display it, but if it doesn't exist then display another image in a different directory? I've written simple "if" statements for text display, but nothing like this. Is this possible?

It might help to tell the scenario. I am putting together templates for teacher web sites. If they wish, they can upload their own logo into their upload directory to display at the top of the page, but if they choose not to upload their own logo I want the default (district) logo to appear there. The default logo is located in another directory on the same server.

Thanks in advance!
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Same concept as text, you probably want to do this, or some variant thereof:

Code: Select all

$target = "path/to/pic.png";

$default = "path/to/default.png";

if(file_exists($target) && is_file($target))

{
	echo "<img src=\"$target\" />";
}

	else

{
	echo "<img src=\"$default\" />";
}
direland
Forum Newbie
Posts: 2
Joined: Wed May 31, 2006 7:24 pm

Post by direland »

Thanks! This worked perfectly!
Post Reply