使用以下代码,可以很好地处理小文件,无论多大(参见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
答案 0 :(得分:0)
我已经走了使用mod_xsendfile的路线,我写了一个单独的脚本,其中包含以下内容:
$the_clip = 'files/Clip/'.$clip_bought->file_url;
header('X-Sendfile: '.$the_clip);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; file="'.$the_clip.'"');
exit;
它提供文件,下载工作,但它将文件作为download.php(脚本的文件名)而不是实际文件的名称。
有没有人知道一个标题,可以让你按照自己的意愿提供文件名?
由于
编辑:
使用Live标头我可以看到它:
HTTP/1.1 200 OK
Date: Tue, 06 Apr 2010 10:42:28 GMT
Server: Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 PHP/5.2.6-1+lenny6 with Suhosin-Patch
X-Powered-By: PHP/5.2.6-1+lenny6
Content-Disposition: attachment; file="movie.wmv"
Last-Modified: Fri, 02 Apr 2010 13:09:32 GMT
Content-Length: 1107113956
Etag: "d8084-41fd37e4-48340b0ab3b00"
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: application/octet-stream
...但仍然以download.php
的形式提供文件