PHP remote file download methods and performance considerations _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP provides three methods for downloading remote files and performance considerations. When exporting an Excel file, you always need to test the exported Excel file and frequently download and open it, it is very troublesome to think about writing code segments to export Excel files on the server in one breath to download Excel files. today, when exporting Excel files, it is always necessary to test the exported Excel files and frequently download and open them.

You just want to write a code segment to export an Excel file to a local location at the server in one breath and then open the Excel file.

Here we will extract the PHP remote file download solution for forgetting. Among them, the 3rd methods take into account the performance issues when the file is too large.

Three solutions:

-Rw-r -- 1 liuyuan 470 Feb 20 18:12 test1_fopen.php

-Rw-r -- 1 liuyuan 541 Feb 20 18:06 test2_curl.php

-Rw-r -- 1 liuyuan 547 Feb 20 18:12 test3_curl_better.php

Solution 1: small files

Use fopen ()/file_get_contents () directly to get the file stream and write it with file_put_contents ()

// An example xls file form baidu wenku

$ Url = 'http: // bs.baidu.com/wenku4/%2fe43e6732eba84a%af36c5c67a7c6d6? Sign = MBOT: y1jXjmMD4FchJHFHIGN4z: response % 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 );

?>

  

Solution 2: get content through Curl

// An example xls file form baidu wenku

$ Url = 'http: // bs.baidu.com/wenku4/%2fe43e6732eba84a%af36c5c67a7c6d6? Sign = MBOT: y1jXjmMD4FchJHFHIGN4z: response % 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 );

?>

  

There is a problem in solution 1 and 2, that is, the file will be read into the memory before being written to the local disk. when the file is large, it may crash if it exceeds the memory.

Even if your memory settings are large enough, this is not an overhead.

Solution: directly give CURL a writable file stream to solve this problem by itself (using the CURLOPT_FILE option). In this way, you must first create a file pointer to it.

// An example xls file form baidu wenku

$ Url = 'http: // bs.baidu.com/wenku4/%2fe43e6732eba84a%af36c5c67a7c6d6? Sign = MBOT: y1jXjmMD4FchJHFHIGN4z: response % 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 );

?>

  

Ghost just wanted to write the code segment and export it to the server in one breath. Excel = download the Excel file...

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.