Implement single-threaded download of images to local ②

Source: Internet
Author: User

------------------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 ②

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.