e1seix Posted June 13, 2008 Share Posted June 13, 2008 Quick query really, is there some sort of if statement we could use to say for example if an image url is invalid to display something else in it's place. eg. image url = "/happy_face.jpg" but there is not such image as "happy_face.jpg", therefore display "default.jpg" or something like that? Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/ Share on other sites More sharing options...
bluejay002 Posted June 13, 2008 Share Posted June 13, 2008 try this: <?php // series of codes ... $picture = 'happy_face.jpg'; // you may also include the path if(!file_exist($picture)) { $picture = 'default.jpg'; } // the rest of the code to load a picture ... ?> cheers, Jay Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/#findComment-564454 Share on other sites More sharing options...
e1seix Posted June 13, 2008 Author Share Posted June 13, 2008 I see what you're getting at, but for me there seems to be a problem - ie. Fatal error: Call to undefined function file_exist(). is there something i'm unaware of, or have i just implemented it incorrectly? $picture = 'images/thumbs/tn_'.$row[image].'.jpg'; // you may also include the path if(!file_exist($picture)) { $picture = 'images/thumbs/tn_default.jpg'; } echo '<div id="bottomImage" style="background:#ffffff; background-image:url('.$picture.'); background-repeat:no-repeat;"><a href="/viewItem.php?sku='.$row[sku].'"><img height="100px" src="/images/thumbs/tn_mini.gif" width="100px" /></a></div>'; what i'm trying to say is that if $picture DOESN'T exist, then display default... what's going on? lol many thanks, Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/#findComment-564826 Share on other sites More sharing options...
Rayhan Muktader Posted June 13, 2008 Share Posted June 13, 2008 Hmm... file_exists() been around since php4. What version are you using? Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/#findComment-564875 Share on other sites More sharing options...
kev wood Posted June 13, 2008 Share Posted June 13, 2008 the if file exist defo work i am using it on a site i have up and running at the minute. here is the code i used $image = ""; $broad_img1= ""; $path= 'http://www.yourdomain.com/'; // sets the first half of the path $web_image_folder = 'image/thumbs/broad/broad1'; // defines the folders to look in $exts = array('jpg', 'png', 'gif', 'jpeg'); // sets up the array $image_name = 'thumb_broad1'; $size = 'width="200" height="200"'; // check for each extension foreach($exts as $ext) { if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) { $image = $image_name.'.'.$ext; } } change it and use it. Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/#findComment-564880 Share on other sites More sharing options...
imdead Posted June 13, 2008 Share Posted June 13, 2008 I see what you're getting at, but for me there seems to be a problem - ie. Fatal error: Call to undefined function file_exist(). is there something i'm unaware of, or have i just implemented it incorrectly? $picture = 'images/thumbs/tn_'.$row[image].'.jpg'; // you may also include the path if(!file_exist($picture)) { $picture = 'images/thumbs/tn_default.jpg'; } echo '<div id="bottomImage" style="background:#ffffff; background-image:url('.$picture.'); background-repeat:no-repeat;"><a href="/viewItem.php?sku='.$row[sku].'"><img height="100px" src="/images/thumbs/tn_mini.gif" width="100px" /></a></div>'; what i'm trying to say is that if $picture DOESN'T exist, then display default... what's going on? lol many thanks, you put file_exist not exists should be $picture = 'images/thumbs/tn_'.$row[image].'.jpg'; // you may also include the path if(!file_exists($picture)) { $picture = 'images/thumbs/tn_default.jpg'; } echo '<div id="bottomImage" style="background:#ffffff; background-image:url('.$picture.'); background-repeat:no-repeat;"><a href="/viewItem.php?sku='.$row[sku].'"><img height="100px" src="/images/thumbs/tn_mini.gif" width="100px" /></a></div>'; Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/#findComment-564886 Share on other sites More sharing options...
bluejay002 Posted June 14, 2008 Share Posted June 14, 2008 try this: <?php // series of codes ... $picture = 'happy_face.jpg'; // you may also include the path if(!file_exist($picture)) { $picture = 'default.jpg'; } // the rest of the code to load a picture ... ?> cheers, Jay my bad... i forgot to add 's' on file_exist()... it should be file_exists(). thanks for the look guys... it should work by now. Quote Link to comment https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/#findComment-565317 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.