PHPfile_put_contents () implements batch download of image files and image code in css _ PHP Tutorial

Source: Internet
Author: User
PHPfile_put_contents () implements batch download of image files and image code in css. Let's take a look at the methods for php to batch download image files and images in css. we will use the file_put_contents () function, next, let's take a look at the first batch download image. let's take a look at the php batch download of image files and the methods for downloading images in css. we will use the file_put_contents () function, next, let's take a look at the first batch download image.
The code is as follows:
Set_time_limit (0); // Set the PHP timeout value.
$ 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. use an array loop containing the image address, and then use the file_get_contents function of PHP to retrieve the image. Then, use the file_put_contents function to save the image.
P.S: you must add the PHP timeout time ~!

In the original text, download the image generation in css using php.Code:

The code is as follows:

<? Php
/*
More & Original PHP Framwork
Copyright (c) 2007-2008 IsMole Inc.
Author: kimi
Documentation
*/

// Note: Set the PHP timeout.
Set_time_limit (0 );

// Note: get the style file content
$ StyleFileContent = file_get_contents ('images/style.css ');

// The note matches the URL to be downloaded.
Preg_match_all ("/url (. *)/", $ styleFileContent, $ imagesURLArray );

// Download the addresses to be downloaded in the note loop one by one
$ ImagesURLArray = array_unique ($ imagesURLArray [1]);
Foreach ($ imagesURLArray as $ imagesURL ){
File_put_contents (basename ($ imagesURL), file_get_contents ($ imagesURL ));
}

?>

Extend


You must download the images in the style file. If you encounter a large style file that may contain hundreds of images to be downloaded, the following small code is the most suitable.

The code is as follows:


<? Php


// Note: Set the PHP timeout.



Set_time_limit (0 );


// Note: get the style file content

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


// The note matches the URL to be downloaded.

Preg_match_all ("/url (. *)/", $ styleFileContent, $ imagesURLArray );


// Download the addresses to be downloaded in the note loop one by one


$ ImagesURLArray = array_unique ($ imagesURLArray [1]);

Foreach ($ imagesURLArray as $ imagesURL ){

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


}

In the end, I wish you all the best in your time.


Definition and usage of file_put_contents
The file_put_contents () function writes a string to a file.

It is the same as calling the fopen (), fwrite (), and fclose () functions in turn.

Syntax
File_put_contents (file, data, mode, context)

Parameters Description
File Required. Specifies the file to write data. If the file does not exist, create a new file.
Data Optional. Specifies the data to be written into the file. It can be a string, array, or data stream.
Mode

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

  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX
Context

Optional. Specifies the file handle environment.

Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored.

You can use the file_put_contents () function to batch download image files and css images. The first batch download image is shown below...

Related Article

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.