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 from the ordinary world blog... 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 blogs in the ordinary world. it can be used after research and rewriting, it is more convenient and convenient.
PHP batch download image file 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 image addresses, 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.
Note: you must add the PHP timeout value.
The following code downloads the image in css using php in the original text:
Later, I found another php file to download images in batches. if I now find that the image storage method on a website is 1001-, all the .jpg images starting from 1 (not accurate to numbers) are saved, now I decided to use php to directly download the image to the local device according to the desired style.
The starting address of the fake film is: http://image.phprm.com/img/1001/1.jpg
Secret, first make sure to create a writable folder named img under the execution directory of this file. the code is as follows:
$ 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 if (preg_match ('/200/', $ array [0]) by returning 400 and 200.) {$ 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 ('All finished '); // display the current status echo $ url ,'-', $ array [0], 'script location. href = "'. $ new_url. '"; script';