Php curl allows you to remotely download files to a local device.
Php curl allows you to remotely download an object to a local device. The result returned by this function includes the download duration, saved file name, and download method. The writing is rough. If you have good suggestions and improvement plans, please leave a message to me!
The Code is as follows:
<? Php // $ result = httpcopy ('HTTP: // www.phpernote.com/image/logo.gif'); echo '<pre>'; print_r ($ result); function httpcopy ($ url, $ file = '', $ timeout = 60) {$ file = empty ($ file )? Pathinfo ($ url, PATHINFO_BASENAME): $ file; $ dir = pathinfo ($ file, PATHINFO_DIRNAME );! Is_dir ($ dir) & @ mkdir ($ dir, 0755, true); $ url = str_replace (''," % 20 ", $ url ); $ result = array ('filename' => '', 'way' =>'', 'SIZE' => 0, 'endtime' => 0 ); $ startTime = explode ('', microtime (); $ startTime = (float) $ startTime [0] + (float) $ startTime [1]; if (function_exists ('curl _ init ') {$ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE ); $ Temp = curl_exec ($ ch); if (@ file_put_contents ($ file, $ temp )&&! Curl_error ($ ch) {$ result ['filename'] = $ file; $ result ['way'] = 'curl '; $ result ['SIZE'] = sprintf ('%. 3f', strlen ($ temp)/1024) ;}} else {$ opts = array ('http' => array ('method' => 'get ', 'header' => '', 'timeout' => $ timeout); $ context = stream_context_create ($ opts); if (@ copy ($ url, $ file, $ context) {$ result ['filename'] = $ file; $ result ['way'] = 'copy '; $ result ['SIZE'] = sprintf ('%. 3f', strlen ($ context)/1024) ;}$ endTime = explode ('', microtime (); $ endTime = (float) $ endTime [0] + (float) $ endTime [1]; $ result ['endtime'] = round ($ endTime-$ startTime) * 1000; // unit: millisecond return $ result ;}
Articles you may be interested in
- Php retrieves remote images and downloads and saves them to a local device.
- PHP uses Curl Functions to capture webpages and download files with multiple threads
- Php methods to determine whether a remote file exists
- Php obtains the remote file size.
- Php reads the Directory and lists the functions that display the files in the directory.
- Php clears (deletes) the files in the specified directory, and does not delete the directory folder.
- Php obtains all the files in the directory and saves the results to the array program.
- Use SecureCRT to upload and download files (using sz and rz commands)