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.