Home > Backend Development > PHP Tutorial > PHP downloads remote images and saves them locally method summary

PHP downloads remote images and saves them locally method summary

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:05:41
Original
1004 people have browsed it

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)
Copy after login

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);
Copy after login

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); 
}
Copy after login

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.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why is this ID written like this?
From 1970-01-01 08:00:00
0
0
0
Small question about $this
From 1970-01-01 08:00:00
0
0
0
This file demo.mp4
From 1970-01-01 08:00:00
0
0
0
javascript - es6中this
From 1970-01-01 08:00:00
0
0
0
What's the use of this?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template