Php method for obtaining remote images and downloading and saving them to a local device

Source: Internet
Author: User
Tags file url
This article mainly introduces php's method of obtaining remote images and downloading and saving them to a local device. it compares and analyzes php's methods for obtaining remote images and saving images based on curl and cache operations in combination with examples, for more information about how to obtain remote images and download and save them to a local device, see the example in this article. We will share this with you for your reference. The details are as follows:

Remote images refer to the data on the remote server. we can use many php functions to read and download the data. here we have sorted out two examples for automatically downloading remote images and saving them to a local device.

Example 1: the image type can be automatically recognized and saved.

/** Function: php perfectly implements remote image download and saving to local * parameter: file url, save file directory, save file name, download method * when the file name is saved as null, use the original name of the remote file */function getImage ($ url, $ save_dir = '', $ filename = '', $ type = 0) {if (trim ($ url) = '') {return array ('File _ name' => '', 'SAVE _ path' => '', 'error' => 1);} if (trim ($ save_dir) ='') {$ save_dir = '. /';} if (trim ($ filename) = '') {// Save the file name $ ext = strrchr ($ url ,'. '); if ($ ext! Using '.gif '& $ ext! Else '.jpg ') {return array ('File _ name' => '', 'SAVE _ path' =>'', 'error' => 3 );} $ filename = time (). $ ext;} if (0! = Strrpos ($ save_dir, '/') {$ save_dir. = '/';} // Create a save directory if (! File_exists ($ save_dir )&&! Mkdir ($ save_dir, 0777, true) {return array ('File _ name' => '', 'SAVE _ path' => '', 'Error' => 5);} // method used to obtain remote files if ($ type) {$ ch = curl_init (); $ timeout = 5; curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout ); $ img = curl_exec ($ ch); curl_close ($ ch);} else {ob_start (); readfile ($ url); $ img = ob_get_contents (); ob_end_clean ();} // $ size = strlen ($ img); // File size $ fp2 = @ fopen ($ save_dir. $ filename, 'A'); fwrite ($ fp2, $ img); fclose ($ fp2); unset ($ img, $ url ); return array ('File _ name' => $ filename, 'SAVE _ path' => $ save_dir. $ filename, 'error' => 0 );}

Example 2: Skip the image if it is not an image

 
'; // If the returned value is true, the else: echo "false"; endif; // otherwise, the output fails to be collected.

In fact, the above download method is not the two most favorite, but the curl is better to save the asynchronous download image.

I hope this article will help you with PHP programming.

For more information about how to obtain remote images from php and download and save the images to your local device, see PHP!

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.