Php retrieves remote images and downloads and saves them to a local device.

Source: Internet
Author: User
Share a function code that uses php to obtain remote images and download and save them to a local device:/** function: php perfectly downloads remote images and saves them to a local device * parameter: file url, save the file directory and save the file name. The download method * when the file name is empty, use the original name of the remote file */functiongetImage ($ url, $ save_dir''

Share a function code that uses php to obtain remote images and download and save them to a local device:

/** 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 );}

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.