sotusotusotu Posted January 3, 2008 Share Posted January 3, 2008 Hi all, For some reason I am having trouble displaying a default image when an image doesn't exist. I have written the following code. Or is there another way of doing this? Thanks <?php if (file_exists ($serverPath."images/".$speakerRecord['image'])) {?> <img src="<?php print $relativePath."images/".$speakerRecord['image'];?>" alt="<?php print $speakerRecord['speaker_name'];?>" class="featured_events_image_l"/> <?php } else { ?> <img src="speaker_large.gif" alt="" /> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/ Share on other sites More sharing options...
adam291086 Posted January 3, 2008 Share Posted January 3, 2008 Whats actually happening. Have you looked at the source code once the code is running to ensure image paths are correct? Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/#findComment-429166 Share on other sites More sharing options...
JamieThompson90 Posted January 3, 2008 Share Posted January 3, 2008 Assuming that speakerRecord is a database result and you are selecting from the returned array this then leaves you with two undefined variables. Both relativePath and serverPath. Can you please show your full source code? Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/#findComment-429169 Share on other sites More sharing options...
sotusotusotu Posted January 3, 2008 Author Share Posted January 3, 2008 The source code for the image is: <img src="images/speakers/" alt="Estelle Hersenzhorn, WRAP" /> The code works fine when the image exists and returns the correct image. The problem just seems to be that it thinks that an image always seems to exist. Any ideas? Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/#findComment-429185 Share on other sites More sharing options...
adam291086 Posted January 3, 2008 Share Posted January 3, 2008 well when there isn't an image do you just get a blank screen? Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/#findComment-429187 Share on other sites More sharing options...
sotusotusotu Posted January 3, 2008 Author Share Posted January 3, 2008 Just the alt tag is displayed in Firefox - so it is correctly getting the data but just not displaying an alternative image. Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/#findComment-429195 Share on other sites More sharing options...
rajivgonsalves Posted January 3, 2008 Share Posted January 3, 2008 this if (file_exists ($serverPath."images/".$speakerRecord['image'])) should be if (file_exists ($serverPath."images/".$speakerRecord['image']) && !is_dir($serverPath."images/".$speakerRecord['image'])) what is happening $speakerRecord['image'] is not coming in and therefore it checking the dir and returning true Link to comment https://forums.phpfreaks.com/topic/84271-if-file-not-exists/#findComment-429197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.