用于文件强制下载的 PHP 标头重定向不起作用


Php header redirecting for file force download not working

我遇到了标头重定向的问题,并在大小大于 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');'

你能试试吗?PHP 脚本强制使用块下载大文件

源代码:

<?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>";?>
?>