PHP bulk Download HTML and CSS image file Instance _php tutorial

Source: Internet
Author: User
In PHP in order to download the first step of the picture I need to use the regular to collect the image address in the string, and then use the PHP related functions to directly read the picture and save to the local server that is to achieve the image batch download.

Recently has been very busy, encountered a manual live, need to download some remote pictures, altogether more than 100 Zhang, if by hand a piece of Save, also too time-consuming, so the internet Google a handful, find php bulk Download image file method, The original text is from the ordinary World blog a piece of how to use PHP bulk download CSS files in the image of the article. After the study has rewritten a bit can be used, convenient and much faster.

PHP Bulk Download image file code:

The code is as follows Copy Code

Set_time_limit (0);//Set PHP time-out
$imagesURLArray = Array_unique ($imagesURLArray);

foreach ($imagesURLArray as $imagesURL) {
Echo $imagesURL;
echo "
";
File_put_contents (basename ($imagesURL), file_get_contents ($imagesURL));
}

The principle is simple, a loop with an array of image addresses, and then use PHP's file_get_contents function to get the picture, using the File_put_contents function to save the picture.
P.S: Be sure to add the PHP time-out period Oh ~!

Attach the source code to download the images in CSS via PHP:

The code is as follows Copy Code

< PHP
/*
More & Original PHP framwork
Copyright (c) 2007-2008 Ismole Inc.
Author:kimi
Documentation: Download the picture in the style file, water special Mr Skinner tool
*/

Note Set PHP timeout time
Set_time_limit (0);

Note Get style file content
$styleFileContent = file_get_contents (' images/style.css ');

Note matches the URL address that needs to be downloaded
Preg_match_all ("/url ((. *))/", $styleFileContent, $imagesURLArray);

Note loops need to download the address, download it individually
$imagesURLArray = Array_unique ($imagesURLArray [1]);
foreach ($imagesURLArray as $imagesURL) {
File_put_contents (basename ($imagesURL), file_get_contents ($imagesURL));
}


Later found a php bulk download picture file

If I now find that the image on a Web site is stored in the 1001–1999 directory is stored in the 1 (the number of different). jpg images, and now I decided to use the PHP method to download the image directly to the local style

Fake piece start address: http://image.xxx.com/img/1001/1.jpg
At this point I put 1001 places in the variable $id,1.jpg to the variable $num.jpg, save the file name is $id_$num.jpg
First make sure to create a folder named IMG and writable under this file execution directory

The code is as follows Copy Code
$id = Isset ($_get[' id ") && intval ($_get[' id ') && $_get[' id ']>1000? $_get[' ID ']: 1001;
$num = isset ($_get[' num ')) && intval ($_get[' num ')? $_get[' num ']: 1;
$url = "http://image.xxx.com/img/{$id}/{$num}.jpg";

$array =get_headers ($url, 1);

By returning 200 and 400来 the judgment is to increase the $id or $num
if (Preg_match ('/200/', $array [0])) {
$new _url= "? id={$id}&num=". ($num + 1);

Ob_start ();
ReadFile ($url);
$img = Ob_get_contents ();
Ob_end_clean ();

$filename = "./img/{$id}_{$num}.jpg";
$f =fopen ($filename, ' a ');
Fwrite ($f, $img);
Fclose ($f);
}else{
$new _url= "? id=". ($id + 1). " &num=1 ";
}
if ($id > 1999) exit (' complete ');
Displays the current status
echo $url, '-', $array [0], ';

http://www.bkjia.com/PHPjc/632712.html www.bkjia.com true http://www.bkjia.com/PHPjc/632712.html techarticle in PHP to download the first step of the picture I need to use the regular to collect the image address in the string, and then use the PHP related functions to read the picture directly and saved to the local server is implemented ...

  • Related Article

    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.