PHP batch download of images in a specified path

Source: Internet
Author: User
Know the path saved by the server slice and how to batch download the class to the local device. The 850 folder and 014 file names both match from 000, and the file will be downloaded if it exists, otherwise skipped: the following code can only download the specified page: (, hope... know the path where the server slice is saved and how to batch download it to the local device. The 850 folder and 014 file names both start matching from 000. If the file exists, it will be downloaded. Otherwise, it will be skipped.

The following code can only download the specified page: (Thank you !)

 

The following code can only download the specified page: (Thank you!

  

Availablecurl_multi_*Batch Request Remote addresses for series functions. Put all the pages to be requested in an array, and send 20 concurrent requests at a time.
We recommend that you modify the program idea. First, obtain the image addresses to be downloaded in batches Based on the page address, and then process the image downloads in batches.
Both parts can be used.culr_multi_*Series functions are processed concurrently.

It's too bad to generate a copy of the code. You can try again later.

Used multiple times in a cycleaddPageThat's all.

If you already have a url, you can use it directly.download.

for ($i = 0; $i < 99; ++$i)    download('http://cdn.image.com/static/' . $i . '.png', DOWNLOAD_PATH . $i . '.png');

The efficiency is a little low. You can use the test token instead.culr_multi_*Series function.

 $referer) {        // echo $url, "\n";        $filename = pathinfo($url, PATHINFO_FILENAME) . '.' . pathinfo($url, PATHINFO_EXTENSION);        download($url, $path . $filename, $referer);    }    // error_reporting(1);}function addPage($page, &$urls) {    $cur = extractPage($page);    for ($i = 0, $n = count($cur); $i < $n; ++$i) {        $j = $cur[$i];        if (!isset($urls[$j]))            $urls[$j] = $page;    }}function extractPage($page, $reg = '//') {    $content = file_get_contents($page);    // $content = '';    preg_match_all($reg, $content, $matches);    return $matches[1];}function download($url, $file, $referer = '') {    $url = abs_url($url, $referer);    echo $url, "\n";    $opts = [        'http' => [            'method' => 'GET',            'header' => "Accept-language: en\r\n" .                         // "Cookie: \r\n" .                        "Referer: " . $url . "\r\n"        ]    ];    $context = stream_context_create($opts);    file_put_contents($file, file_get_contents($url, false, $context));    return $file;}function abs_url($url, $referer) {    $com = parse_url($url);    if (!isset($com['scheme']))        $com['scheme'] = 'http';    if ($referer !== '') {        $ref = parse_url($referer);        if (!isset($com['host'])) {            if (isset($ref['host'])) {                $com['host'] = $ref['host'];                if (!isset($ref['path']))                    $ref['path'] = '/';            }            if (isset($com['path'][0])) {                if ($com['path'][0] !== '/')                    $com['path'] = $ref['path'] . $com['path'];            } else if (isset($ref['host'])) {                $com['path'] = $ref['path'];            }        } else {            if (!isset($com['path']))                $com['path'] = '';        }    }    return unparse_url($com);}function unparse_url($com) {    return (isset($com['host']) ? ($com['scheme'] . '://' . $com['host']) : '') . $com['path'] . (isset($com['query']) ? '?'.$com['query'] : '');}

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.