PHP 3 ways to download remote files and performance considerations

Source: Internet
Author: User
Tags curl php download
Today in doing export Excel, always test the exported Excel file, frequent download and open, very troublesome   just want to write segment code coherent   service side export excel==> Download the Excel file to the local ==> and open the operation.   Here to get a PHP download remote file scheme, in case to forget. The 3rd of these methods takes into account the performance problems when the file is too large.           3 options:  -rw-rw-r--1 Liuyuan liuyuan 470 Feb 18:12 test1_fopen.php-rw-rw-r--1 Liuyuan Liuyuan 541 Feb 18:06 test2_curl.php-rw-rw-r--1 Liuyuan Liuyuan 547 Feb 18:12 test3_curl_better.php ;     Scenario 1, for small files   direct use of fopen ()/file_get_contents () to get file streams and file_put_contents () write   <?php    //an example xls file form Baidu Wenku     $url = ' http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c 67a7c6d6?sign=mbot:y1jxjmmd4fchjhfhign4z:lfzax1nrf44acyd6tjqj2fhosly%3d&time=1392893977& Response-content-disposition=attachment;%20filename=%22php%ba%af%ca%fd.xls%22&response-content-type= Application%2foctet-stream ';       $FP _input = fopen ($url, ' R ');     file_put_contents ('./test.xls ', $fp _input);     &NBSp EXEC ("LibreOffice./test.xls", $out, $status);?>   Scenario 2: Getting content through Curl     <?php    //an EX Ample xls file form Baidu Wenku     $url = ' http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6? sign=mbot:y1jxjmmd4fchjhfhign4z:lfzax1nrf44acyd6tjqj2fhosly%3d&time=1392893977& Response-content-disposition=attachment;%20filename=%22php%ba%af%ca%fd.xls%22&response-content-type= Application%2foctet-stream ';       $ch = Curl_init ($url);     curl_setopt ($ch, Curlopt_returntransfer, true);     file_put_contents ('./test.xls ', curl_exec ($ch));     Curl_close ($ch);       EXEC ("LibreOffice./test.xls", $out, $status);?>   1th, one problem with 2 scenarios is that files are read into memory before they are written to the local disk. When a file is large, it may crash out of memory   even if your memory settings are large enough, that's not the cost.   The solution is to simply give curl a writable file stream to solve the problem by itself (via the curlopt_file option), This will first create a file pointer to it.     <?php    //an example xls file form Baidu Wenku   &NBSp $url = ' http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z: lfzax1nrf44acyd6tjqj2fhosly%3d&time=1392893977&response-content-disposition=attachment;%20filename=% 22php%ba%af%ca%fd.xls%22&response-content-type=application%2foctet-stream ';       $FP _output = fopen ('./test.xls ', ' W ');     $ch = Curl_init ($url);     curl_setopt ($ch, Curlopt_file, $fp _output);     curl_exec ($ch);     Curl_close ($ch);          EXEC ("LibreOffice/test.xls", $out, $status);?>

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.