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?

Link to comment
Share on other sites

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 " ";

}

 

 

Link to comment
Share on other sites

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.

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.