Php curl allows you to remotely download files to a local device.

Source: Internet
Author: User

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)

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.