Home >Backend Development >PHP Tutorial >PHP batch download image files in html and css examples_PHP tutorial

PHP batch download image files in html and css examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:36862browse

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
 代码如下 复制代码

set_time_limit(0);//设置PHP超时时间
$imagesURLArray = array_unique($imagesURLArray );

foreach($imagesURLArray as $imagesURL) {
    echo $imagesURL;
    echo "
";
    file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}

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));
 代码如下 复制代码

< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.
Author: kimi
Documentation: 下载样式文件中的图片,水水专用扒皮工具
*/

//note 设置PHP超时时间
set_time_limit(0);

//note 取得样式文件内容
$styleFileContent = file_get_contents('images/style.css');

//note 匹配出需要下载的URL地址
preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray);

//note 循环需要下载的地址,逐个下载
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
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 text: <🎜><🎜>
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
代码如下 复制代码
$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);
 
//通过返回200和400来判断是增加$id或者$num
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/{$id}_{$num}.jpg";
 $f=fopen($filename,'a');
 fwrite($f,$img);
 fclose($f);
}else{
 $new_url="?id=".($id+1)."&num=1";
}
if($id > 1999) exit('全部完成');
//显示当前的状态
echo $url,' - ',$array[0],'<script>location.href="'.$new_url.'";</script>';

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";

//Judge whether to increase $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/{$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...
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