PHP batch download examples of html and css image files

Source: Internet
Author: User

The first step to download images in php is to use regular expressions to collect the image addresses in strings, then, you can use php-related functions to directly read images and save them to the local server, which enables batch download of images.

I have been very busy recently. I have encountered a manual activity and need to download some remote images. There are more than one hundred images in total. If I save them manually, it will take too much time, so I Googled the internet, find the PHP Method for batch downloading image files. The original article is an article about how to use PHP to batch download images in CSS files from ordinary world blogs. After research and rewriting, you can use it, which is more convenient and convenient.

PHP batch download image file code:

The Code is as follows: Copy code

Set_time_limit (0); // set the PHP timeout value.
$ ImagesURLArray = array_unique ($ imagesURLArray );

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

The principle is very simple. Use an array loop containing the image address, and then use the file_get_contents function of PHP to retrieve the image. Then, use the file_put_contents function to save the image.
P.S: you must add the PHP timeout time ~!

The following code downloads the image in css using php in the original text:

The Code is as follows: Copy code

<? Php
/*
More & Original PHP Framwork
Copyright (c) 2007-2008 IsMole Inc.
Author: kimi
Documentation
*/

// Note: Set the PHP timeout.
Set_time_limit (0 );

// Note: Get the Style File Content
$ StyleFileContent = file_get_contents ('images/style.css ');

// The note matches the URL to be downloaded.
Preg_match_all ("/url (. *)/", $ styleFileContent, $ imagesURLArray );

// Download the addresses to be downloaded in the note loop one by one
$ ImagesURLArray = array_unique ($ imagesURLArray [1]);
Foreach ($ imagesURLArray as $ imagesURL ){
File_put_contents (basename ($ imagesURL), file_get_contents ($ imagesURL ));
}


Then I found another php file to download images in batches.

Now, if I find that the image storage method on a website is 1001-, and all the .jpg images starting from 1 (the number is not equal to the number) are saved, I decided to use php to directly download the images to the local device according to the style I needed.

The starting address of the fake film is: http://image.xxx.com/img/1001/1.jpg
Bytes
First, make sure to create a writable folder named img under the execution directory of this file.

The Code is as follows: Copy code
<? Php
$ 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 );
 
// You can add $ id or $ num by returning 200 and 400.
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/{$id1__{num=.jpg ";
$ F = fopen ($ filename, 'A ');
Fwrite ($ f, $ img );
Fclose ($ f );
} Else {
$ New_url = "? Id = ". ($ id + 1)." & num = 1 ";
}
If ($ id> 1999) exit ('all finished ');
// Display the current status
Echo $ url, '-', $ array [0], '<script> location. href = "'. $ new_url. '"; </script> ';

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.