Jump to content


Photo

Show a default image if no image exists?


  • Please log in to reply
3 replies to this topic

#1 direland

direland
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 12:23 AM

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!

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 01 June 2006 - 12:32 AM

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]

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

~ D Kuang

#3 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 01 June 2006 - 01:07 AM

here is the if statement i use and it works great


$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>";
}


The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#4 direland

direland
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 03:46 AM

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


$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>";
}

[/quote]

Thanks! This works perfectly!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users