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>

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.