PHP Download File_put_contents vs ReadFile

Source: Internet
Author: User
Tags php download ranges
Set_time_limit (0);
Ini_set (' Memory_limit ', ' 512M ');
Get current time
function GetTime ($convert = True)
{
Return Microtime ($convert);
}
Get current Memory
function memory ()
{
return Memory_get_usage ();
}
$m = Memory ();
$s = getTime ();
$file = file_get_contents ("A.zip", true);
Header ("Content-type:application/octet-stream");
Header ("Accept-ranges:bytes");
Header ("Accept-length:".) FileSize (' A.zip '));
Header (' content-disposition:attachment; filename= ' A.zip ');
Echo $file;
Error_log (' File_put_contents: Memory: '. ( Memory ()-$m). ', time-consuming: '. (GetTime ()-$s). ' \ r \ n ', 3, ' d:/1.txt ');
/* ReadFile () Benchmark */
$m = Memory ();
$s = getTime ();
Ob_start ();
Header (' Content-type:application/octet-stream ');
Header ("Accept-ranges:bytes");
Header ("Accept-length:".) FileSize (' A.zip '));
Header (' content-disposition:attachment; filename= ' A.zip ');
Header (' content-transfer-encoding:binary ');
ReadFile (' A.zip ');
Ob_end_clean ();

Error_log (' ReadFile: Memory: '. ( Memory ()-$m). ', time-consuming: '. (GetTime ()-$s). ' \ r \ n ', 3, ' d:/1.txt ');

The results are as follows:

2k file
File_put_contents: Memory: 2304, Time: 0.00099992752075195
ReadFile: Memory: 888, TIME: 0.002000093460083
20M file
File_put_contents: Memory: 50126256, Time: 0.21602201461792
ReadFile: Memory: 896, Time: 0.071007013320923
153M file
File_put_contents: Memory: 321370872, Time: 1.0341031551361
ReadFile: Memory: 840, Time: 1.9421939849854

Conclusion: Using ReadFile to save memory when downloading large files under the same conditions is not easy to cause PHP memory overflow problem.

The above describes the PHP download file_put_contents vs ReadFile, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

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