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); 
flyclassic
  • 2,459
  • 6
  • 30
  • 44

1 Answers1

5

Perform a HEAD request and check the response code.

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