Check whether image exists on remote URL Check whether image exists on remote URL php php

Check whether image exists on remote URL


function checkRemoteFile($url){    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL,$url);    // don't download content    curl_setopt($ch, CURLOPT_NOBODY, 1);    curl_setopt($ch, CURLOPT_FAILONERROR, 1);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    $result = curl_exec($ch);    curl_close($ch);    if($result !== FALSE)    {        return true;    }    else    {        return false;    }}

--> that is the fastest way if your host supports curl


Use getimagesize() method like this

$external_link = ‘http://www.example.com/example.jpg’;if (@getimagesize($external_link)) {echo  “image exists “;} else {echo  “image does not exist “;}


There is no "easy" way here - at a very minimum, you need to generate a HEAD request and check the resulting content type to make sure it's an image. That's not taking into account possible referrer issues. curl is the way to go here.