------------------The foundation is not good first look at the previous article to find the feeling--------------
1. Get the image URL
$file _contents = file_get_contents ("http://www.cnblogs.com/yaradish/archive/2018/08/21.html" ); Preg_match_all ('/\w+:\/\/[\w\.] +\/\w+\/\w+\/\w+\/[\w\.] + (. Jpg|. PNG)/', $file _contents, $pics);
$pic = $pics [0];
In order to facilitate, grabbed a page of the picture link to test, the regular may need to modify, if the regular can not understand, directly write the URL link array, as follows: The following image links are the same, to find some pictures of the link
$pic = [ https://images2018.cnblogs.com/blog/1436248/201808/ 1436248-20180819133735730-1667186904.png, https://images2018.cnblogs.com/blog/1436248/ 201808/1436248-20180819133735730-1667186904.png, https://images2018.cnblogs.com/blog/ 1436248/201808/1436248-20180819133735730-1667186904.png, https:// Images2018.cnblogs.com/blog/1436248/201808/1436248-20180819133735730-1667186904.png];
2, set the start time (this step can not write, but print and record the start time)
$beginTime =Time (); $lastTime=$beginTime; $count=0; Echo'begin download at'. Date'y-m-d h:i:s', $beginTime).'<br/>'; Set_time_limit ( -); #页面超时处理echo Date ('h:i:s', time ())."<br/>"; $timeout=5; # Single Picture time limit
3. Grab picture Data information
foreach($pic as$value) {$ch=Curl_init (); curl_setopt ($ch, Curlopt_url, $value); curl_setopt ($ch, Curlopt_returntransfer,1); curl_setopt ($ch, Curlopt_header,0); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); curl_setopt ($ch, Curlopt_ssl_verifypeer,false); curl_setopt ($ch, Curlopt_ssl_verifyhost,false); $file _contents=curl_exec ($ch); #打这里位置和单张图片处理一模一样 curl_close ($ch); $picname=pathinfo ($value, pathinfo_basename); #获取图片名称 $picdir="./single"; if(!Is_dir ($picdir)) {mkdir ($picdir,0777); #建立文件夹存放图片} $savename=$picdir. $picname; $file= fopen ("./single/". $savename,'W'); #建立图片存放图片信息 fwrite ($file, $file _contents); Fclose ($file); $nowTime=Time (); #后面的也可以不写都是时间显示及消耗时间 $takeTime= $nowTime-$lastTime; ++$count; Echo'downloaded'. $count.'---------------'. $takeTime.'s<br/>'; $lastTime=$nowTime; Echo $picname."<br/>";}
4. Total time Consumed
$endTime =Time (); $totalTime= $endTime-$beginTime; Echo'End Download at'. Date'y-m-d h:i:s', $endTime).'<br/>'; Echo'downloaded'. $count.'Pictures Take time'. $totalTime.'s<br/>';
5. Go to the current directory in the single directory to view the downloaded pictures. Over
Implement single-threaded download of images to local ②