php下载在大文件上失败

时间:2014-10-01 21:14:04

标签: php

我试图获取下载文件的链接而不重定向,或使用其他文件下载大型zip文件。

它适用于~100mb以下的文件,但更大的文件会被发送到chrome中的错误页面,上面写着Error code: ERR_INVALID_RESPONSE

将$ file和$ path放入带有jQuery的表单中,然后调用$('form').submit();

function downloadFile($file, $path){

        set_time_limit(0);
        $fullPath = $path . $file;

        if ($fd = fopen ($fullPath, "r")) {


            $fsize = filesize($fullPath);
            $path_parts = pathinfo($fullPath);



            $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"]."\"");
            }
            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;
    }

为什么它在较大的文件上失败的任何想法?

更新: 当我使用以下代码时,它会下载该文件,但由于某些原因,已完成的文件已损坏,因此已打开:

set_time_limit(0);
ini_set('memory_limit', '1024M');
// http headers for downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$file_size);
ob_end_flush();
@readfile($fileinfo['path']);   

当我使用此代码时,它只会下载前49.7kb,然后说它已完成,我甚至一起尝试ob_flush()flush()

    // http headers for downloads
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($fileinfo['path']));

    if ($fd = fopen ($fileinfo['path'], "r")) {

            set_time_limit(0);
            ini_set('memory_limit', '1024M');

        while(!feof($fd)) {
            echo fread($fd, 4096);
            flush();
        }   

    }

1 个答案:

答案 0 :(得分:4)

我在下载程序中唯一不同的是Content-Transfer-Encoding: chunked。同时将flush()移出循环,确保在发送数据之前ob_clean(); flush();ob_end_flush();之后执行此操作。