Robust Download Function in PHP? -


i'm trying download image file programatically within php , treat locally.

edited: previous function replace 1 suggested below.

i have function:

function downloadfile ($url, $path) {     $result = false;     $useragent = 'mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; .net clr 1.1.4322)';      $ch = curl_init($url);     curl_setopt($ch, curlopt_useragent, $useragent);      curl_setopt($ch, curlopt_connecttimeout, 60);      curl_setopt($ch, curlopt_followlocation, true);      curl_setopt($ch, curlopt_ssl_verifypeer, false);     curl_setopt($ch, curlopt_ssl_verifyhost, false);      curl_setopt($ch, curlopt_nobody, true);     curl_setopt($ch, curlopt_header, true);     curl_exec( $ch ) ;     if(!curl_errno($ch))       {         $type = curl_getinfo($ch, curlinfo_content_type);          if ( stripos($type, 'image') !== false )         {             // image              curl_setopt($ch, curlopt_returntransfer, 1);             curl_setopt($ch, curlopt_nobody, false);               curl_setopt($ch, curlopt_header, false);                 $fp=fopen($path,'wb');             curl_setopt($ch, curlopt_file, $fp);              curl_exec($ch);             fclose($fp);             if ( exif_imagetype($path) != false )             {                 // 100% image                 $result = true;             }             else             {                 // not image                 unlink($path);             }         }     }      curl_close($ch);     return $result; } 

what need function robust , can deal type of image , if url invalid , there no image.

update:

i changed downloadfile function 1 suggested below. on local computer works great, on server fails :/ i'm having files downloaded 0 bytes.

update2:

still no progress, in server reason files not downloaded. besides having curl, there other requirements ran in server? "2006 - mysql server has gone away", believe caused download problem.

use curl.

this function checks url image also. returns true/false (image or not).

function downloadfile ($url, $path) {     $result = false;     $useragent = 'mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; .net clr 1.1.4322)';      $ch = curl_init($url);     curl_setopt($ch, curlopt_useragent, $useragent);      curl_setopt($ch, curlopt_connecttimeout, 60);      curl_setopt($ch, curlopt_followlocation, true);      curl_setopt($ch, curlopt_ssl_verifypeer, false);     curl_setopt($ch, curlopt_ssl_verifyhost, false);      curl_setopt($ch, curlopt_nobody, true);     curl_setopt($ch, curlopt_header, true);     curl_exec( $ch ) ;     if(!curl_errno($ch))       {         $type = curl_getinfo($ch, curlinfo_content_type);          if ( stripos($type, 'image') !== false )         {             // image              curl_setopt($ch, curlopt_returntransfer, 1);             curl_setopt($ch, curlopt_nobody, false);               curl_setopt($ch, curlopt_header, false);                 $fp=fopen($path,'wb');             curl_setopt($ch, curlopt_file, $fp);              curl_exec($ch);             fclose($fp);             if ( exif_imagetype($path) != false )             {                 // 100% image                 $result = true;             }             else             {                 // not image                 unlink($path);             }         }     }      curl_close($ch);     return $result; } 

request:

if ( !downloadfile($url, $path) ) {      // error }  

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -