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

Jul 13, 2016 am 10:49 AM
css html php download and picture exist Example I batch document of want need

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Deconstructing H5 Code: Tags, Elements, and Attributes Deconstructing H5 Code: Tags, Elements, and Attributes Apr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

See all articles