PHPCURL implements remote file download to local _ PHP Tutorial

Source: Internet
Author: User
PHPCURL allows you to remotely download an object to a local device. PHPCURL allows you to remotely download a file to a local PHPCURL to remotely download the file to a local location. the result returned by this function includes the download duration, saved file name, php curl is used to remotely download files to the 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:

 '; 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)

Http://www.bkjia.com/PHPjc/992743.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/992743.htmlTechArticlePHP CURL realize remote download file to local php curl realize remote download file to local, the function returns the result including download time consumed, save the file name, and use the download side...

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.