Home  >  Article  >  Backend Development  >  How to batch download image files through PHP_PHP Tutorial

How to batch download image files through PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:54:041186browse

I have been very busy recently. I encountered a manual job and needed to download some remote pictures. There were more than a hundred pictures in total. It would be too time-consuming to save them one by one manually, so I went online and googled them. I found a way to download image files in batches with PHP. The original article is about how to use PHP to batch download images in CSS files. After some research and rewriting, it can be used, which is much more convenient and faster.

PHP batch download image file code:

set_time_limit(0);//Set PHP timeout
$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, loop through an array containing the image address, then use PHP's file_get_contents function to obtain the image, and then use the file_put_contents function to save the image.
P.S: Be sure to set the PHP timeout~!

Attached is the code for downloading images in css through php in the original article:

< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.
Author: kimi bkjia.com
Documentation: Download the pictures in the style file, Shuishui special peeling tool
*/

//note Set PHP timeout
set_time_limit(0);

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

//note Matching needs to be downloaded URL address
preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray);

//note Loop through the addresses to be downloaded and download them one by one
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364691.htmlTechArticleI have been very busy recently. I encountered a manual job and needed to download some remote pictures. There were more than a hundred pictures in total. It would be too time-consuming to save one by one manually, so I went online to goo...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn