PHP batch download examples of html and css Image files

Source: Internet
Author: User
Tags php file

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.

Assume that the image start address is yun_qi_img/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 = "yun_qi_img/{$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> ';
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.