direland Posted June 1, 2006 Share Posted June 1, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/10906-show-a-default-image-if-no-image-exists/ Share on other sites More sharing options...
poirot Posted June 1, 2006 Share Posted June 1, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/10906-show-a-default-image-if-no-image-exists/#findComment-40743 Share on other sites More sharing options...
shortj75 Posted June 1, 2006 Share Posted June 1, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10906-show-a-default-image-if-no-image-exists/#findComment-40755 Share on other sites More sharing options...
direland Posted June 1, 2006 Author Share Posted June 1, 2006 [!--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!! Quote Link to comment https://forums.phpfreaks.com/topic/10906-show-a-default-image-if-no-image-exists/#findComment-40780 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.