作者previa (Southern Cross)
看板PHP
标题Re: [请益] readfile 效率问题
时间Mon Jul 17 05:14:10 2006
※ 引述《KoShiyen (http://0rz.net/e70jv)》之铭言:
: 我写了一个程式过滤下载的使用者
: 通过的才会用 readfile 放档案给他, 否则会改放拒绝的图档
: 可是本来直接下载时很快很容易下载的档
: 透过 readfile 就变成慢得不得了
: 开一个档常常要等好几分钟才开始下载
: 用不同的 webhost 也是得到同样的结果
: 请问这是设定的问题还是程式的问题?
: 用 readfile 释出档案前需要什麽特别的处理吗?
: 谢谢.
这是我在别的地方看到的 我也有这个困扰 XD
http://theserverpages.com/php/manual/en/function.readfile.php
regarding php5:
i found out that there is already a disscussion @php-dev about readfile()
and fpassthru() where only exactly 2 MB will be delivered.
so you may use this on php5 to get lager files
<?php
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.140.55.7
※ 编辑: previa 来自: 220.140.55.7 (07/17 05:14)
※ 编辑: previa 来自: 220.140.55.7 (07/17 05:16)
1F:推 KoShiyen:我也有看到这篇, 用这个方法确实快很多 07/17 11:48
2F:→ KoShiyen:可惜还是比不上直接 embed 的速度 不过还是感谢您 orz 07/17 11:49
3F:推 kenshieh:大型档案建议都用此方法 因为 readfile 会把整个档案先 07/17 16:59
4F:→ kenshieh:读到记忆体 之後再写入 所以一定很慢 07/17 17:00
5F:→ kenshieh:这方法 io 虽然多次 但一次读的少自然而然就快多了 07/17 17:00