Jump to content

Archived

This topic is now archived and is closed to further replies.

direland

Show a default image if no image exists?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
You can check if the image file exists with is_file()
[a href=\"http://www.php.net/is_file\" target=\"_blank\"]http://www.php.net/is_file[/a]

[code]if (is_file($img)) {
   echo '<img src="' . $img . '" alt=" ">';
} else {
   echo '<img src="default.gif">';
}[/code]

Share this post


Link to post
Share on other sites
here is the if statement i use and it works great

[code]

$filename="../pic.gif";
if(file_exists($filename))
{
         echo "<img src=../$filename target=_blank>";
   }
else
{
  echo "<img src=../if no pic.gif height=100 width=100>";
}

[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=378906:date=May 31 2006, 09:07 PM:name=shortj75)--][div class=\'quotetop\']QUOTE(shortj75 @ May 31 2006, 09:07 PM) [snapback]378906[/snapback][/div][div class=\'quotemain\'][!--quotec--]
here is the if statement i use and it works great

[code]

$filename="../pic.gif";
if(file_exists($filename))
{
         echo "<img src=../$filename target=_blank>";
   }
else
{
  echo "<img src=../if no pic.gif height=100 width=100>";
}

[/code]
[/quote]

Thanks! This works perfectly!!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.