PHP 下载 file_put_contents vs readfile
2016-07-06 12:58
603 查看
set_time_limit(0);
ini_set('memory_limit', '512M');
//获取当前时间
function getTime($convert = true)
{
return microtime($convert);
}
//获取当前内存
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()-$m).',耗时:'.(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()-$m).',耗时:'.(getTime()-$s).'\r\n',3,'D:/1.txt');
结果如下:
2k文件
file_put_contents: 内存:2304,耗时:0.00099992752075195
readfile: 内存:888,耗时: 0.002000093460083
20M文件
file_put_contents: 内存:50126256,耗时:0.21602201461792
readfile: 内存:896,耗时:0.071007013320923
153M文件
file_put_contents: 内存:321370872,耗时:1.0341031551361
readfile: 内存:840,耗时:1.9421939849854
结论:相同条件下在下载大文件时使用readfile更省内存,不容易导致php内存溢出问题。
ini_set('memory_limit', '512M');
//获取当前时间
function getTime($convert = true)
{
return microtime($convert);
}
//获取当前内存
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()-$m).',耗时:'.(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()-$m).',耗时:'.(getTime()-$s).'\r\n',3,'D:/1.txt');
结果如下:
2k文件
file_put_contents: 内存:2304,耗时:0.00099992752075195
readfile: 内存:888,耗时: 0.002000093460083
20M文件
file_put_contents: 内存:50126256,耗时:0.21602201461792
readfile: 内存:896,耗时:0.071007013320923
153M文件
file_put_contents: 内存:321370872,耗时:1.0341031551361
readfile: 内存:840,耗时:1.9421939849854
结论:相同条件下在下载大文件时使用readfile更省内存,不容易导致php内存溢出问题。
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 关于浮动与清除浮动,你应该知道的
- php7 读取php.ini[4]
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得