简体   繁体   中英

can't get file_exists to work

I'm trying to do something that seems simple: If a user would like to change a picture, they can, and if there is no picture, they can upload one. I want to determine first whether a photo already exists. If it does, it's filename is site_id.jpg. The variable $site_id connects the correct photo with the correct user. Here is my code:

<?php
$image_exists="";
$image_exists=file_exists('/home/tippc0/public_html/observations_images/160/'.$site_id.'.jpg');

if ($image_exists) {
    echo $image_exists;
    echo $site_id;
?>
<img src="/home/tippc0/public_html/observations_images/160/'.$site_id.'.jpg" />

The echo statements are there for troubleshooting, and the <img src> is there to show me the photo, again just for troubleshooting.

So, HERE'S THE PROBLEM: "file_exists" says a file exists (returns 1) even through none exists. I know none exists because (a) there is no file in the database, and (b) I get the standard "no image file exits" icon (the square with the question mark in it) in place of the photo.

If I make up a name for the file instead of of using '.site_id'.jpg , it returns false as it should. If there is no filename and just the path ending in /160/ , file_exists returns true, presumably because the directory "160" exists.

Why is file_exists saying a file exists when it doesn't?????

Your web server will not be able to access the /home/tippc0 directory. It will navigate from the web server root which may be the public_html folder so it's looking for the file here

/home/tippc0/public_html/observations_images/160/

but trying to output the file from

/home/tippc0/public_html/home/tippc0/public_html/observations_images/160/

which it can't find so your image is broken when it tries to display it. To fix this, make sure the file is stores in a place that can be accessed from the web server. If public_html is your web server root, make your img tag like this:

<img src="/observations_images/160/<?php echo $site_id; ?>.jpg" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM