1

I want to check if a remote image exist. Currently using CURL and based on its response to i can determine if the file exist. However, this takes a fare bit amount of time depending on the image file size also.

Is there a safest and cheapest way of doing a check without doing it offline or using cache?

$ch = curl_init(); // set URL and other appropriate options /*--Note: if in sabre proxy, please activate the last 4 lines--*/ $options = array( CURLOPT_URL => $img, CURLOPT_HEADER => false, // get the header CURLOPT_NOBODY => true, // body not required CURLOPT_RETURNTRANSFER => false, // get response as a string from curl_exec, not echo it CURLOPT_FRESH_CONNECT => false, // don't use a cached version of the url CURLOPT_FAILONERROR => true, CURLOPT_TIMEOUT => 5 ); curl_setopt_array($ch, $options); if(!curl_exec($ch)) { return FALSE; } $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return ($httpcode<400); 
2
  • You can send HEADER instead of GET to only know what would the response be without downloading the file, but how to do it in curl I don't know. Commented Oct 3, 2011 at 5:18
  • @Dani: it's HEAD, not HEADER. Commented Oct 3, 2011 at 5:19

1 Answer 1

5

Perform a HEAD request and check the response code.

Sign up to request clarification or add additional context in comments.

2 Comments

Then there's nothing you can do. The server is just slow.
@flyclassic: using CURLOPT_NOBODY issues a HEAD request, that's why they're the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.