views:

915

answers:

7

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?

A: 

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.

Jaimal Chohan
We're talking about many websites, not under my control, so the possibility of a web service is excluded.
Cristi Cotovan
+6  A: 

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.

ChssPly76
A: 

I think there is a way you can check remote image url exists or not. this might helps you

http://shailkpatel.blogspot.com/2009/10/check-whether-image-exists-on-remote.html

shailesh
A: 

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?

Andrew Deal
+1  A: 

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

Kevin Peno
A: 

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

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

 return($fp);
}
ChronoFish
Try to avoid the at-operator.
Török Gábor
A: 

this simple javascript method worked just perfect for me http://patelshailesh.com/index.php/check-whether-image-exists-on-remote-url

mayavi