7

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?

1

8 Answers 8

21

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Ahh you're a genius man, just like php include files... Can't believe I didn't even try that. Thanks a lot!!
Another tricky moment - U can use file_exists('./images/filename.jpg'); PHP knows where web-root folder is through ./, this is much comfortable if ex.: U may set a new dir for web server to work with, then the /home/var/www/images/ might not be a solution U need.
Another useful thing to always find the image even in included php files is to use __DIR__. So something like file_exists(__DIR__ . '/../image/image.jpg'); Helps ensure the correct path no matter what.
@johnsnails I am guessing that as long as you are sure that the current directory won't be changed with chdir somewhere above (now or in the future, by you or someone else) it should be fine.
5

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

1 Comment

Or $_SERVER['CONTEXT_DOCUMENT_ROOT'] even if your alias is not under your document root.
1

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

Comments

1

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

Comments

0

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

Comments

0

file_exists doesn't support remote URLs.

You can try this:

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

Comments

0

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;" />';
}

Comments

0

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.