PHP file_put_contents () Implementation of bulk download picture files and CSS in the picture Code _php tutorial

Source: Internet
Author: User
Let's start with a brief introduction about PHP bulk download image files and CSS in the method of the image, we will use the file_put_contents () function, the following see the first batch download pictures.
The code is as follows Copy Code
Set_time_limit (0);//Set PHP time-out
$imagesURLArray = Array_unique ($imagesURLArray);

foreach ($imagesURLArray as $imagesURL) {
Echo $imagesURL;
echo "
";
File_put_contents (basename ($imagesURL), file_get_contents ($imagesURL));

The principle is simple, loop through an array of image addresses, then use PHP's file_get_contents function to get the picture and save the picture using the File_put_contents function.
P.S: Be sure to add the PHP time-out period Oh ~!

attach the code in the original text to download the image in CSS via PHP :

The code is as follows Copy Code

< PHP
/*
More & Original PHP framwork
Copyright (c) 2007-2008 Ismole Inc.
Author:kimi
Documentation: Download the picture in the style file, water special Mr Skinner tool
*/

Note Set PHP timeout time
Set_time_limit (0);

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

Note matches the URL address that needs to be downloaded
Preg_match_all ("/url ((. *))/", $styleFileContent, $imagesURLArray);

Note loops need to download the address, download it individually
$imagesURLArray = Array_unique ($imagesURLArray [1]);
foreach ($imagesURLArray as $imagesURL) {
File_put_contents (basename ($imagesURL), file_get_contents ($imagesURL));
}

?>

Extends to


In the Mr Skinner process, it is essential to download the pictures in the style file. When you encounter a larger style file, which may have hundreds of images to download, it is most appropriate to use this little code.

copy code


< php


//note set PHP time-out



Set_time_limit (0);


//note Gets the contents of the style file

$styleFileContent = file_get_contents (' images/style.css ');


//note matches the URL address

preg_match_all ("/url (. *)/") that needs to be downloaded, $styleFileContent, $imagesURLArray);


//note the addresses that need to be downloaded, download


$imagesURLArray = Array_unique ($imagesURLArray [1]);

foreach ($imagesURLArray as $imagesURL) {

File_put_contents (basename ($imagesURL), file_get_contents ($ Imagesurl));


}

Finally, I wish you a Mr Skinner in the process, a grilled in the end


About file_put_contents Definitions and usage
The file_put_contents () function writes a string to the file.

Same as calling fopen (), fwrite (), and fclose () in turn.

Grammar
File_put_contents (File,data,mode,context)

Parameters Description
File Necessary. Specifies the file to write the data to. If the file does not exist, a new file is created.
Data Optional. Specifies the data to write to the file. Can be a string, an array, or a data stream.
Mode

Optional. Specifies how to open/write files. Possible values:

  • File_use_include_path
  • File_append
  • Lock_ex
Context

Optional. Specifies the environment for file handles.

The context is a set of options that modify the behavior of the stream. If NULL is used, it is ignored.

http://www.bkjia.com/PHPjc/631635.html www.bkjia.com true http://www.bkjia.com/PHPjc/631635.html techarticle let's start with a brief introduction about PHP bulk download image files and CSS in the method of the image, we will use the file_put_contents () function, the following see the first batch download pictures ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.