47

For some reason this PHP code below will not work, I can not figure it out.

It is very strange, file_exists does not seem to see that the image does exist, I have checked to make sure a good file path is being inserted into the file_exists function and it is still acting up

If I change file_exists to !file_exists it will return an images that exist and ones that do not exist

define('SITE_PATH2', 'http://localhost/');

$noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg';
$thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if (file_exists($thumb_name)) {
    $img_name = $thumb_name;
}else{
    $img_name = $noimg;
}
echo $img_name;
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Take a note here, if it may help someone. I use command with same user to check a file exists works well, while not works from the browser(php-fpm). I've checked related config in php.ini, php-fpm.conf, www.conf, even do modification, but not works. At last I found the config in nginx fastcgi.conf, which will overwrite the config in php. /usr/local/nginx/conf/fastcgi.conf `fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/:/data/www/";` – LF00 Jul 17 '23 at 03:32

7 Answers7

103

file_exists() needs to use a file path on the hard drive, not a URL. So you should have something more like:

$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if(file_exists($thumb_name)) {
    some_code
}

http://us2.php.net/file_exists

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
AvatarKava
  • 15,245
  • 2
  • 27
  • 33
  • $_SERVER['CONTEXT_DOCUMENT_ROOT'] would work if your alias is not under your document root, in which case $_SERVER['DOCUMENT_ROOT'] will fail – Gavin Simpson Oct 28 '13 at 07:48
  • I was aware of the file path/url that gets most people. But if you are still having an issue, don't forget to check basic things, for example, I was using an alias for the path in my framework but forgot to resolve the alias to the actual path in the check. – johnsnails Jul 26 '19 at 13:07
9

docs say:

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 2
    I don't believe HTTP/HTTPS are on the list of stat() supported protocols per the docs - just some "goofier" stuff like php://memory. – AvatarKava Aug 17 '09 at 13:08
8

file_exists does only work on the local file system.

So try this if you’re using localhost:

$thumb_name = 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if (file_exists($_SERVER['DOCUMENT_ROOT'].$thumb_name)) {
    $img_name = SITE_PATH2.$thumb_name;
} else {
    $img_name = $noimg;
}
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
Gumbo
  • 643,351
  • 109
  • 780
  • 844
2

Have you enabled the option which allows you to use external URLs? You can set it in php.ini:

allow_url_fopen = 1
1

You have to write the file path like "file:///C:/Documents%20and%20Settings/xyz/Desktop/clip_image001.jpg".

Asaph
  • 159,146
  • 25
  • 197
  • 199
Mannusanghi
  • 249
  • 1
  • 4
  • 9
0

http://php.net/manual/en/function.file-exists.php

did you check the comments below?

Just reading parts of it, but there seem to be several issues.

Caching may be a problem. When opening FTP urls it always returns true (they say in the comments) ...

StampedeXV
  • 2,715
  • 2
  • 24
  • 40
0

Try Below one. Its working for me

define('SITE_PATH2', 'http://localhost/');
$noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg';
$thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';

if ($fileopen = @fopen($thumb_name)) {
    $img_name = $thumb_name;
    fclose($fileopen);
}else{
    $img_name = $noimg;
}
echo $img_name;