Php obtains remote images and downloads them to a local device ** function: php perfectly downloads remote images and saves them to a local device * parameter: file url, save file directory, save file name, download method used * when the file name is saved as null, use the original remote file name * functiongetImage ($ url, $ save_dir, $ filename, $ type0) {if (trim ($ url) php obtains the remote image and downloads it to the 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 [email protected] ($ save_dir. $ filename, 'A'); fwrite ($ fp2, $ img); fclose ($ fp2); unset ($ img, $ url ); return array ('File _ name' => $ filename, 'SAVE _ path' => $ save_dir. $ filename, 'error' => 0 );}