【问题标题】:Serving files (800MB) results in an empty file提供文件 (800MB) 会产生一个空文件
【发布时间】: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 内存分配?它可能会在下载文件之前尝试将文件缓冲到内存中,这会遇到一些限制?

标签: php apache download


【解决方案1】:

我已经走上了使用 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 header 我可以看到它的作用:

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 提供

【讨论】:

  • 经过研究,第 4 行的 $the_clip 现在是文件的名称,而不是它的路径,但是它仍然作为 download.php 提供,我猜它一定是 mod_xsendfile 问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 2016-12-10
  • 2018-09-25
相关资源
最近更新 更多