1 File download class (Supports resumable downloads) 1). Function: supports resumable downloads, can calculate the transfer rate, and can control the transfer rate
Easy to use:function getFileSize($url){ $url = parse_url($url); if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)){ fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n"); fputs($fp,"Host:$url[host]\r\n\r\n"); while(!feof($fp)){ $tmp = fgets($fp); if(trim($tmp) == ''){ break; }else if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)){ return trim($arr[1]); } } return null; }else{ return null; } } echo getFileSize(http://www.dianpub.com/download/xml.rar)
Class file:
//记录程序开始的时间 $BeginTime=getmicrotime(); function GrabImage($url,$filename="") { if($url==""):return false;endif; if($filename=="") { $ext=strrchr($url,"."); if($ext!=".gif" && $ext!=".jpg"):return false;endif; $filename=date("dMYHis").$ext; } ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2=@fopen($filename, "a"); fwrite($fp2,$img); fclose($fp2); return $filename; } $img=GrabImage("http://www.dianpub.com/images/_1978837_detector_ap100.jpg",""); if($img):echo '<pre class="brush:php;toolbar:false"><img src="'.$img.'">';else:echo "false";endif; //记录程序运行结束的时间 $EndTime=getmicrotime(); //返回运行时间 exit($EndTime-$BeginTime);
5. php uses GD library to download remote images
if(!empty($saveremoteimg)) { $body = stripslashes($body); $img_array = array(); preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array); $img_array = array_unique($img_array[2]); set_time_limit(0); $imgUrl = $img_dir."/".strftime("%Y%m%d",time()); $imgPath = $base_dir.$imgUrl; $milliSecond = strftime("%H%M%S",time()); if(!is_dir($imgPath)) @mkdir($imgPath,0777); foreach($img_array as $key =>$value) { $value = trim($value); $get_file = @file_get_contents($value); $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3); $fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3); if($get_file) { $fp = @fopen($rndFileName,"w"); @fwrite($fp,$get_file); @fclose($fp); } $body = ereg_replace($value,$fileurl,$body); } $body = addslashes($body); }
Note that you need to increase the memory allocated by PHP and use a large memory server when applying it
The above has introduced a summary of the method of downloading remote images and saving them locally in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.