Cristian Cotovan
Cristian Cotovan

Reputation: 1090

Check whether image exists on remote URL

I am generating dynamic URLs of images for book ISBNs. I need a reliable way with PHP to check whether the images actually exist at the remote url. I tried various approaches with different PHP libraries, curl, etc., but none of them works well, some of them are downright slow. Given the fact that I need to generate (and check!) about 60 URLS for each book in my database, this is a huge waiting time. Any clues?

Upvotes: 62

Views: 126869

Answers (10)

mohsin139
mohsin139

Reputation: 1027

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

Upvotes: 71

Deepak Kumar
Deepak Kumar

Reputation: 64

I am using this and modified from all other answers by myself. Exit or not exit image but we also have to handle error which is missing in other answers.

Here is what I am using :

function checkRemoteFile($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects

    // Set a maximum timeout of 10 seconds to prevent the script from hanging
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    // Execute the request and get the HTTP status code
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    // Check the HTTP status code
    if($httpCode >= 200 && $httpCode < 300) {
        // The file exists and the server returned a successful HTTP status code
        return true;
    } else {
        // The file does not exist or the server returned an error HTTP status code
        return false;
    }
}

Upvotes: 0

ChronoFish
ChronoFish

Reputation: 3707

It's probably moot at this point, but this works for me:

function is_webfile($webfile)
{
 $fp = @fopen($webfile, "r");
 if ($fp !== false)
  fclose($fp);

 return($fp);
}

Upvotes: 1

Gr Brainstorm
Gr Brainstorm

Reputation: 179

Solution from https://www.experts-exchange.com

<?php
function url_exists($url) {
    if (!$fp = curl_init($url)) return false;
    return true;
}
?>

Upvotes: 0

dangkhoaweb
dangkhoaweb

Reputation: 1199

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

Upvotes: 119

Andrew Deal
Andrew Deal

Reputation: 91

I have been doing this for my real estate picture tracking...

$im = @imagecreatefromjpeg($pathtoimg);
if($im)
  imagedestroy($im); // dont save, just ack...
elseif(!$missing[$inum])
  $img404arr[] = $inum;

It 'seems' faster than downloading the actual image, taking about .3 seconds for each from images that avg 100k.

I wish I could just do a header check and read whether I get a 200 vs a 404 without downloading anything. Anyone have that handy?

Upvotes: 4

Kevin Peno
Kevin Peno

Reputation: 9196

You could use curl. Just set the curl option CURLOPT_NOBODY to true. This will skip body information and only get the head (thus http code as well). Then, you could use the CURLOPT_FAILONERROR to turn this whole process into a true/false type check

Upvotes: 4

ChssPly76
ChssPly76

Reputation: 100706

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.

Upvotes: 6

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

If the images all exist on the same remote server (or in the same network), you could run a web service on that server that will check the file system for the the image file and return a bool value indicating wheter the image exists or not.

Upvotes: -1

Related Questions