【发布时间】:2010-03-26 16:33:06
【问题描述】:
使用以下代码,可以很好地处理小文件,但大文件(请参阅 800MB 及以上)会导致空文件!
我需要用 apache 做点什么来解决这个问题吗?
<?php
class Model_Download {
function __construct($path, $file_name) {
$this->full_path = $path.$file_name;
}
public function execute() {
if ($fd = fopen ($this->full_path, "r")) {
$fsize = filesize($this->full_path);
$path_parts = pathinfo($this->full_path);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
}
}
编辑:如果我使用
fpassthru($fd); exit;
相反,我在文件中写入以下内容:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 786032641 bytes) in /Users/aaron/Sites/com/library/Model/Download.php on line <i>44
【问题讨论】:
-
文件下载后的内容是什么?尝试用文本编辑器打开一个。
-
它似乎正在传输该数据?用 whireshark 嗅探以进一步诊断。
-
它会生成一个空文件,在文本编辑器中打开它会显示其写入的是一个空文件。我现在将研究 whireshark。
-
嗯,现在我在想它,你有 error_reporting to E_ALL 和 display_error on 吗?
-
什么是 PHP 内存分配?它可能会在下载文件之前尝试将文件缓冲到内存中,这会遇到一些限制?