0

In my project, when user log in from Facebook, the profile picture will be saved. It is working. But if there is no image, then also a 0 Byte file is saving.

How can I check the url contains image and save only if the image is available

The PHP sample code is given below

function DownloadImage($url, $dest){
    $curl = curl_init($url);
    $fp = fopen($dest, 'wb');
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($curl);   
    curl_close($curl);
    fclose($fp);    
}

try{ 
    $type='large';  
    $social_id='1234';
    DownloadImage('https://graph.facebook.com/'.$social_id.'/picture?type='.$type.'&access_token=[your access token]', $type.'.jpg');


}
catch(Exception $errr){
    echo $errr;
}
Arun
  • 3,640
  • 7
  • 44
  • 87

1 Answers1

2

You can always check curl returned header info with curl_getinfo() (more at php.net)

So in your case you can curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD) to see how much bytes are returned.

Also i think facebook should return different HTTP code if no image found, so you can try curl_getinfo($curl, CURLINFO_HTTP_CODE). It should be 200 if image found and 404 if not.

Gvidas
  • 1,964
  • 2
  • 14
  • 21
  • Thank you for the reply. But it is showing some strings in the page like `0����JFIF���Photoshop 3.08BIM�g1SQQYlr`. Why it is so? – Arun Sep 21 '16 at 05:53
  • @Arun: That is the start of the binary data of a JPEG image. So you must be outputting the received data somewhere. – CBroe Sep 21 '16 at 07:18