Jump to content

how do i display an image only if it exists


mikefrederick

Recommended Posts

How can I display an image w/ php only if it exists. the images are uploaded via a user, and the image is placed in a folder called propertyimages and the image name is placed in a table called image1 inside of a database. then when it comes time for them to be displayed, I am connecting to the database and using the following script to show the image:

 

<? echo '<img src="propertyimages/'.$row_Rs['imageone'].'" height="200" width="200" vspace="10" hspace="10" >'; ?>

 

now how can I alter this so that it will only show if it actually exists?

I do not know how to use that function in this case. It detects that it exists everytime I think. My problem basically is that the page that the images are displayed on displayes boxes with x's where the image should be if it was not uploaded .I tried the following with no success:

 

$filename = 'propertyimages/'.$_row_Rs['imageone'];

 

if (file_exists($filename)) { echo '<img src="propertyimages/'.$row_Rs['imageone'].'" height="200" width="200" vspace="10" hspace="10" >'; }

else {

    echo " ";

}

 

 

I would just check if there is a file name in the database and then if there is... output the <img> tag accordingly.

 

That would have to assume your upload code works correctly.

 

To me that seems easier then trying to check if a file exists.

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.