61
if (!(file_exists(http://example.com/images/thumbnail_1286954822.jpg))) {   
$filefound = '0';                         
}

why won't this work?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
anonymous
  • 639
  • 1
  • 5
  • 5
  • http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php – ArK Nov 23 '10 at 06:58

7 Answers7

90
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {   
$filefound = '0';
}
Boba Fit
  • 819
  • 6
  • 17
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • 1
    if (!file_exists($base_url.'images/thumbnail_1286954822.jpg')) { $filefound = '0'; } it's like this actually sorry. – anonymous Nov 23 '10 at 06:59
  • 3
    @anonymous actually it's not like this sorry, you didn't ask for $base_url + images/thumbnail_1286954822.jpg you asked for http:// mysite com/images/thumbnail_1286954822.jpg – Robert Pounder Apr 22 '15 at 08:16
36
  1. The function expects a string.

  2. file_exists() does not work properly with HTTP URLs.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
20

file_exists checks whether a file exist in the specified path or not.

Syntax:

file_exists ( string $filename )

Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.

$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
    echo "File exist.";
}else{
    echo "File does not exist.";
}

Another alternative method you can use getimagesize(), it will return 0(zero) if file/directory is not available in the specified path.

if (@getimagesize($filename)) {...}
IRSHAD
  • 2,855
  • 30
  • 39
8

You can also use PHP get_headers() function.

Example:

function check_file_exists_here($url){
   $result=get_headers($url);
   return stripos($result[0],"200 OK")?true:false; //check if $result[0] has 200 OK
}

if(check_file_exists_here("http://www.mywebsite.com/file.pdf"))
   echo "This file exists";
else
   echo "This file does not exist";
Pradeep Kumar
  • 4,065
  • 2
  • 33
  • 40
6

Based on your comment to Haim, is this a file on your own server? If so, you need to use the file system path, not url (e.g. file_exists( '/path/to/images/thumbnail.jpg' )).

JJJ
  • 32,902
  • 20
  • 89
  • 102
3

for me also the file_exists() function is not working properly. So I got this alternative solution. Hope this one help someone

$path = 'http://localhost/admin/public/upload/video_thumbnail/thumbnail_1564385519_0.png';

    if (@GetImageSize($path)) {
        echo 'File exits';
    } else {
        echo "File doesn't exits";
    }
Nashir
  • 254
  • 1
  • 5
  • 9
  • @Nguaial The getimagesize() function will determine the size of any supported given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type. – Nashir Aug 20 '20 at 10:53
2

Check code below

if ($user->image) {
        $filename = "images/" . $user->image;
        if (file_exists($filename)) {
            echo '<br />';
            echo "File exist.";
        } else {
            echo '<br />';
            echo "File does not exist.";
        }
    }
ahmad saad
  • 21
  • 3