The first step to download images in PHP is to use regular expressions to collect the image addresses in the string, and then use PHP related functions to read the images directly and save them to the local server to achieve batch downloading of images.
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 googled them online. Find a way to download image files in batches with PHP. The original text is an article from Ordinary World Blog 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:
The code is as follows | Copy code | ||||||||
$imagesURLArray = array_unique($imagesURLArray ); echo $imagesURL; echo " "; File_put_contents(basename($imagesURL), file_get_contents($imagesURL));
|
The code is as follows | Copy code |
<🎜>< ?php<🎜> /*<🎜> More & Original PHP Framwork<🎜> Copyright (c) 2007 - 2008 IsMole Inc.<🎜> Author: kimi<🎜> Documentation: Download the pictures in the style file, Shuishui special peeling tool<🎜> */<🎜> <🎜>//note Set PHP timeout<🎜> set_time_limit(0);<🎜> <🎜>//note Get the content of the style file<🎜> $styleFileContent = file_get_contents('images/style.css');<🎜> <🎜>//note Match the URL address that needs to be downloaded<🎜> 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));<🎜> }<🎜> |
Later I found a php batch download image file
Suppose now I find that the image saving method on a website is 1001 – 1999. There are .jpg images starting from 1 (varying quantities) stored in the directory. Now I decide to use PHP to save the images according to my needs. Styles are downloaded directly to local location
If the starting address of the image is: http://image.xxx.com/img/1001/1.jpg
At this time, I put 1001 in the variable $id, 1.jpg in the variable $num.jpg, and the saved file name is $id_$num.jpg
First make sure to create a writable folder named img under the execution directory of this file
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"; $f=fopen($filename,'a'); fwrite($f,$img); fclose($f);
}else{
$new_url="?id=".($id+1)."&num=1";
}
if($id > 1999) exit('all completed');
//Show current status
echo $url,' - ',$array[0],'<script>location.href="'.$new_url.'";</script>';
http://www.bkjia.com/PHPjc/632712.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632712.htmlTechArticleIn the first step of downloading images in php, I need to use regular rules to collect the image address in the string, and then Then use php related functions to directly read and save the image to the local server...
|