mackin Posted December 16, 2011 Share Posted December 16, 2011 I have directories containing images. If an Image called front.jpg exists then I want that to display - if it doesn't then I want it to display any random image from the directory. All the bits works individually. but for some reason, even when front.jpg exists - it still displays a random image from the else statement. Can you see an error? $frontimage = "/hotelimages/". $img_dir . "/" . $front . $ext; ?> <div id="topframe"> <?php if (file_exists($frontimage)) {?><img src="/image.php?width=245&height=245&cropratio=1:1&image=/<?php echo $img_folder.$image ;?>" alt="<?php echo ucwords($row_rs_properties['est_name']);?>" class="fltrt" border="1"/><? ;} else { ?> <img src="/image.php?width=245&height=245&cropratio=1:1&image=<?php echo "/hotelimages/". $img_dir . "/" . $front . $ext; ?>" alt="<?php echo ucwords($row_rs_properties['est_name']);?>" class="fltrt" border="1"/> <?php ;} ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/253309-if-else-not-working-correctly-but-cant-spot-error/ Share on other sites More sharing options...
QuickOldCar Posted December 16, 2011 Share Posted December 16, 2011 <?php $frontimage = "/hotelimages/". $img_dir . "/" . $front . $ext; ?> <div id="topframe"> <?php if (file_exists($frontimage)) { ?> <img src="/image.php?width=245&height=245&cropratio=1:1&image=/<?php echo $img_folder.$image ;?>" alt="<?php echo ucwords($row_rs_properties['est_name']);?>" class="fltrt" border="1"/> <?php } else { ?> <img src="/image.php?width=245&height=245&cropratio=1:1&image=<?php echo "/hotelimages/". $img_dir . "/" . $front . $ext; ?>" alt="<?php echo ucwords($row_rs_properties['est_name']);?>" class="fltrt" border="1"/> <?php } ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/253309-if-else-not-working-correctly-but-cant-spot-error/#findComment-1298546 Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2011 Share Posted December 16, 2011 file_exists operates on a file system path. A leading slash on a file system path refers to the root of the current hard disk. The path/filename that you supply to file_exists needs to be either an absolute or relative file system path (these are not the same as absolute or relative URL's.) You can form an absolute file system path by using $_SERVER['DOCUMENT_ROOT'] to get the file system path to your document root folder, then append the correct path/filename to that value. Quote Link to comment https://forums.phpfreaks.com/topic/253309-if-else-not-working-correctly-but-cant-spot-error/#findComment-1298563 Share on other sites More sharing options...
jotorres1 Posted December 16, 2011 Share Posted December 16, 2011 Have you tried debugging this? Print out the string that is in $frontimage, you might be surprised. Second, I would definitely take out those semicolons inside the if brackets. Quote Link to comment https://forums.phpfreaks.com/topic/253309-if-else-not-working-correctly-but-cant-spot-error/#findComment-1298571 Share on other sites More sharing options...
mackin Posted December 16, 2011 Author Share Posted December 16, 2011 Thx people - changed code to this <?php $rooot = $_SERVER['DOCUMENT_ROOT']; $frontimage = "/hotelimages/". $img_dir . "/" . $front . $ext; if (!file_exists($rooot . $frontimage)) { ?> <img src="/image.php?width=245&height=245&cropratio=1:1&image=/<?php echo $img_folder.$image ;?>" alt="<?php echo ucwords($row_rs_properties['est_name']);?>" class="fltrt" border="1"/> <?php } else { ?> <img src="/image.php?width=245&height=245&cropratio=1:1&image=<?php echo "/hotelimages/". $img_dir . "/" . $front . $ext; ?>" alt="<?php echo ucwords($row_rs_properties['est_name']);?>" class="fltrt" border="1"/> <?php } ?> seems the document root was at issue thx PFMaBiSmAd - also removed the semi colons Quote Link to comment https://forums.phpfreaks.com/topic/253309-if-else-not-working-correctly-but-cant-spot-error/#findComment-1298597 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.