PHP curl enables remote download of files to local _php tutorials

Source: Internet
Author: User

PHP curl enables remote download of files to local


PHP curl enables remote download of files to local, the function returns results including the time of download, the name of the saved file, and the way to download it. Write more rough, if you have good suggestions and improve the program, welcome to leave a message to me Oh!

The specific code is as follows:

 ';p Rint_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 ' = ' = ', ' The ' ' "' = ' = ', ' size ' =>0, ' spendtime ' =>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 [' On ' the ']= ' 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 [']= ' copy '; $result [' Size ']=sprintf ('%.3f ',        strlen ($context)/1024); }} $endTime =explode (' ', Microtime ()), $endTime = (float) $endTime [0]+ (float) $endTime [1]; $result [' Spendtime ']=round ($ endtime-$startTime) *1000;//unit: milliseconds return $result;}

Articles you may be interested in

    • PHP get remote picture and download save to local
    • PHP uses Curl Functions to crawl Web pages and download files in multiple threads
    • PHP to determine if a remote file exists
    • PHP Gets the size of the remote file
    • PHP reads directories and tables displays the functions of files in the directory
    • PHP Empty (delete) the file under the specified directory, do not delete the Directory folder method
    • PHP gets all the files in the directory and saves the results to an array of programs
    • Upload and download files using SECURECRT (with SZ and RZ commands)

http://www.bkjia.com/PHPjc/992743.html www.bkjia.com true http://www.bkjia.com/PHPjc/992743.html techarticle PHP Curl Implementation of remote download files to local PHP curl implementation of remote download files to local, the function returns the results include the time of the download, the saved file name, and the use of the download party ...

  • Related Article

    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.