3 Ways to consider PHP download remote files from a performance _php tutorial

Source: Internet
Author: User
Tags php download php framework

3 Ways to consider PHP download remote files from a performance aspect


Today in the export of Excel, always to test the exported Excel file, frequent download and open, very troublesome to write a segment code one GO server export excel==> download Excel file to local ==> and open the operation.

Here is the solution to download the remote file from PHP for forgetting. The 3rd approach takes into account performance issues 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

Get the file stream directly using fopen ()/file_get_contents () and write it with file_put_contents ()

<?php  //an Example 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 ';  $fp _input = fopen ($url, ' R ');  File_put_contents ('./test.xls ', $fp _input);  EXEC ("LibreOffice/test.xls", $out, $status);? >

Scenario 2: Get content through curl

<?php  //an Example 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, there is a problem with 2 scenarios where a file is read into memory before it is written to the local disk, and when the file is large, it may crash beyond memory

Even if your memory settings are large enough, that's not the cost.

The workaround is to give curl a writable file stream to solve this problem by itself (via the curlopt_file option) so that a file pointer is created first.

<?php  //an Example 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 ';  $fp _output = fopen ('./test.xls ', ' W ');  $ch = Curl_init ($url);  curl_setopt ($ch, Curlopt_file, $fp _output);  Curl_exec ($ch);  Curl_close ($ch);  

The above content to introduce you from the performance aspects of PHP download remote file 3 ways, I hope you like.

Articles you may be interested in:

    • PHP Download remote file Class (support breakpoint continuation)
    • PHP with password feature and download remote file save locally specified directory modify the enhanced version
    • High performance PHP Framework Symfony2 Classic Introductory Tutorial
    • Comparison and analysis of file_get_contents and curl performance in PHP
    • PHP download remote file to local storage method

http://www.bkjia.com/PHPjc/1086658.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086658.html techarticle from the performance aspects of PHP download remote file 3 ways, today in the export of Excel, always to test the exported Excel file, frequent download and open, very troublesome to write a paragraph ...

  • 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.