Today in doing export Excel, always to test the exported Excel file, frequent download and open, very troublesome to write a section of code coherent server export excel==> download Excel files to the local ==> and open the operation.
Here to draw out PHP download the 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 Kinds of programs:
-rw-rw-r--1 Liuyuan Liuyuan 470 Feb 18:12
-rw-rw-r--1 Liuyuan Liuyuan 541 Feb 18:06
-rw-rw-r--1 Liuyuan Liuyuan 547 Feb 18:12
Scenario 1, suitable for small files
Get the file stream directly using fopen ()/file_get_contents () and write to 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, the problem with 2 scenarios is that the file is read into memory before it is written to the local disk, so that when the file is large, it may crash beyond memory.
Even if your memory is set to large enough, that's not a cost.
The solution is to give curl a writable file stream to solve the problem by itself (through the curlopt_file option), so that you first create a file pointer to it.
<?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);
EXEC ("LibreOffice/test.xls", $out, $status);
The above content for you to introduce from the performance of PHP to download the remote files of 3 ways, I hope you like.