简体   繁体   中英

PHP file_exists not working

I'm trying to make profile photos show up against a list of reviews on a site I'm working on. If they don't have a profile photo I have a standard image to show instead, unfortunately the image always goes to the standard image rather than the profile even if it exists. Heres the code:

$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
    if(file_exists($reviewerPic)){
        $reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
    }else {
        $reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}

Pretty generic code but it doesn't seem to work! It just keeps showing the background image...

Any ideas on why file_exists wouldn't be working?

The function you are using, file_exists , uses physical paths, the parameter you need to provide should be the address on that server where the file can be found, and not an url

Sou you should have something like

/home/var/www/images/

instead of

http://www.[URL].co.uk/images/

So you need to check if the file exists on the server locally and after that you can use an url to make it available to the public (in img src)

You can see on the man page that this function only works with some URL wrappers, so it is better to use paths and not urls (I guess it depends on allow url fopen setting)

file_exists does not work with HTTP,

use : $_SERVER["DOCUMENT_ROOT"] to access root in your host

so

$reviewerPic = $_SERVER["DOCUMENT_ROOT"].'/images/members/' . $reviewPosterId .  '/profilePic.jpg';
$default_pic = $_SERVER["DOCUMENT_ROOT"].'/images/background.jpg';

File_exists uses the /home/www/username/public_html/ format. You may want to retrieve this from your host.

So it would be something like /home/www/jack/public_html/[URL].co.uk/profilepic/id/profilePic.jpg .

By the way, no need to use . in your echo '<img src=... . You can use variables inside of strings, you should only use the . when you need to modify it using a function or something, like "string0".function($str....)."string1";

According to the PHP manual for file_exists not all protocols supports the function:

this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

The HTTP wrapper does not support stat:

Supports stat() No

the file_exists PHP function works only on physical files like /var/www/file.ext or C:\\WWW\\File.ext and not for HTTP Files. It doesn't even know how to handle HTTP files.

In this case, if you want to know if the file is there or not, since it is going to be an HTTP Response, you can try using curl , which if it responds with 404 , then the file is not found.

For more information, check out these: http://php.net/manual/en/function.file-exists.php and http://php.net/manual/en/book.curl.php

file_exists doesn't support remote URLs.

You can try this:

if (fopen($reviewerPic, "r"))
   echo "File exists!";

Try simple one.

$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
    if($fileopen = @fopen($reviewerPic)){
        $reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
       fclose($fileopen);
    }else {
        $reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}

A great way to handle it is here ~

<?php

$imgok="1.webp"; // Your actual image
$imgerr="template.webp"; // If image is missing
$dir="/assets/image/products/"; // Location of the image directory

$path = $_SERVER['DOCUMENT_ROOT'].$dir. $imgok; // Full Paths for processing

echo file_exists($path) ? $dir.$imgok : $dir.$imgerr; // Actual paths for domain to get the image

?>

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