Jump to content

If else not working correctly but can't spot error.


mackin

Recommended Posts

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>

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.