Php标题重定向文件强制下载不起作用

时间:2015-03-15 20:02:56

标签: php

我遇到了大小超过1GB的文件重定向和强制下载的问题。

此代码适用于大小小于1GB的文件

ini_set('memory_limit', '18364M');

header('Content-Type: application/bin');

header('Content-Description: File Transfer');

header('Content-Disposition: attachment; filename="download.bin"');

readfile('Setup.bin');'

1 个答案:

答案 0 :(得分:0)

你能试试吗? PHP Script to force download big files using chunk

源代码:

<?php
$file = dirname(__FILE__) . "/Setup.bin";
if (file_exists($file)) {
    if (FALSE !== ($handler = fopen($file, 'r'))) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($file));
        header('Content-Transfer-Encoding: chunked'); //changed to chunked
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');

        //Send the content in chunks
        while (false !== ($chunk = fread($handler, 4096))) {
            echo $chunk;
        }
    }
    exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";?>
?>
相关问题