Rocco The Taco
Rocco The Taco

Reputation: 3797

PHP file_get_contents with if/else

On this link I was learning about file_get_contents and I'd like to incorporate it into a simple if/else statement but can't seem to get it correct.

I'd like to check if the file exists and if not, display a no_image.jpg graphic.

My struggle is that the tag already has a PHP echo so it is causing the snippet to stop working. How do I format the if/else while still haveing the use the dynamic value from the echo if file_get_contents is true?

Currently I have a simple tag like this:

<img width="200" height="150" src="images/<?php echo(rawurlencode($row['MLS_NUMBER'])); ?>_1.jpg" alt="" align="left" vspace="3" hspace="3" />

Upvotes: 0

Views: 944

Answers (2)

ksorbo
ksorbo

Reputation: 25

Assuming you are looking for a jpg file based on $row['MLS_NUMBER']

<img width="200" height="150" src="images/
<?php 
 if (file_exists($row['MLS_NUMBER']) {
       echo(rawurlencode($row['MLS_NUMBER'])).'_1.jpg';
     } else {
       echo "no_image.jpg";
     }
 ?>" alt="" align="left" vspace="3" hspace="3" />

Upvotes: 0

Marc B
Marc B

Reputation: 360812

Basically:

<?php

if (file_exists(...)) {
  $path = ...;
} else {
  $path = 'no_image.jpg';
}
?>

<img src="images/<?php echo $path; ?>" />

//add a semicolon after echo $path otherwise it won't work

Upvotes: 6

Related Questions