Jump to content

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!
Link to comment
https://forums.phpfreaks.com/topic/10906-show-a-default-image-if-no-image-exists/
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]
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]
[!--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!!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.