Mr Chris Posted April 30, 2008 Share Posted April 30, 2008 Hi All, Say I have a database record which holds an image name <img src="http://www.gdgdgdgsks.co.uk/images/<?php echo $row['media_image']; ?> The page will obviously be able to load the image, but say the image can't be found and it is broken ie the above outputs as: <img src="http://www.gdgdgdgsks.co.uk/images/55.jpg Which is a broken image. Is there any in which I can use something in PHP which says if it's a broken image then output a default image (despite 55.jpg being the value saved in the database) Thanks Chris Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/ Share on other sites More sharing options...
phpcodec Posted April 30, 2008 Share Posted April 30, 2008 It's not the code that is wrong, it's your image tags Your Code: <img src="http://www.gdgdgdgsks.co.uk/images/55.jpg"> Correct Code: <img src="http://www.gdgdgdgsks.co.uk/images/55.jpg Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530141 Share on other sites More sharing options...
Mr Chris Posted April 30, 2008 Author Share Posted April 30, 2008 No sorry, That's defo not the case, just my poor typing above(!). Anyone know if I can do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530152 Share on other sites More sharing options...
mescal Posted April 30, 2008 Share Posted April 30, 2008 i would strip the .jpg in my database and put that in my php code like <img src="http://www.gdgdgdgsks.co.uk/images/<?php echo $row['media_image']; ?>.jpg"> does this help? grtZ mescal Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530155 Share on other sites More sharing options...
Mr Chris Posted April 30, 2008 Author Share Posted April 30, 2008 Thanks, but I can't do it like that. I'm running a parser which exports the filenames as filename.jpg and saves it in my database. Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530160 Share on other sites More sharing options...
mescal Posted April 30, 2008 Share Posted April 30, 2008 try this code <img src="http://www.gdgdgdgsks.co.uk/images/<?php echo $row['media_image']; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530163 Share on other sites More sharing options...
Wolphie Posted April 30, 2008 Share Posted April 30, 2008 Check if the row exists in the database, if it returns true; Check the database against the file system, i.e. check if the file actually exists in the file system. If either return false, specify a default image otherwise output the correct image. Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530165 Share on other sites More sharing options...
MadTechie Posted April 30, 2008 Share Posted April 30, 2008 try this <?php $path = "http://www.gdgdgdgsks.co.uk/images/"; $img = $row['media_image']; if(!file_exists($path.$img)) { $img = "broken.jpg"; } echo "<img src=\"$path.$img\">"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530169 Share on other sites More sharing options...
mescal Posted April 30, 2008 Share Posted April 30, 2008 watch for the closing tags of your img src Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530172 Share on other sites More sharing options...
Mr Chris Posted April 30, 2008 Author Share Posted April 30, 2008 Hi Guys, Thanks for the reply. Here's how I have it: <?php $path = "http://www.frededeeee.com/images/resize_image.php?image=http://www.frededeeee.com/images/properties"; $img = $prop['media_image_00']; if(!file_exists($path.$img)) { $img = "broken.jpg"; } echo "<img src=\"$path/$img\" class=\"flb\" />"; ?> So 1) The value for 'media_image_00' in the database is outputted 2) The system then checks to see if the file exists 3) However, with the above if there is an image it still shows a broken.jpg image. If I take out the if statement like so: <?php $path = "http://www.frededeeee.com/images/resize_image.php?image=http://www.frededeeee.com/images/properties"; $img = $prop['media_image_00']; if(!file_exists($path.$img)) { $img = "broken.jpg"; } echo "<img src=\"$path/$img\" class=\"flb\" />"; ?> The code works. Now I think there is something wrong with the if statement: if(!file_exists($path.$img)) Needs to be something like: if(!file_exists($path/$img)) However if I use that it tries to use divide? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530203 Share on other sites More sharing options...
MadTechie Posted May 1, 2008 Share Posted May 1, 2008 the path is missing the / so add the / at the end or try this if(file_exists($path."/".$img)) Quote Link to comment https://forums.phpfreaks.com/topic/103534-broken-images-in-php/#findComment-530992 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.