Fre*_*ang 5 php http-headers force-download
我正在尝试下载.mp4文件.(约1.3GB大小).我正在使用以下内容:
<?php 
$path = "video.mp4";
header('Accept-Ranges: bytes');  // For download resume
header('Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header('Content-Description: File Transfer' );
header('Content-Disposition: attachment; filename="'.basename( $path ).'"' );
header('Content-Length: ' . filesize($path));  // File size
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Content-Type: application/octet-stream' );
header('Expires: 0' );
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Pragma: no-cache' );
ob_clean();
flush();
readfile($path);
我打开我的php文件,firefox弹出"想要保存"菜单.尺寸看起来正确.我按"另存为",到我的桌面.最终下载的文件,以随机大小列出,大约400MB(330,463和440).
响应标题是:
Connection: Keep-Alive
Content-Disposition:    attachment; filename="//www.frederikspang.dk/elevgallavideo.mp4"
Content-Length: 1422778850
Content-Type:   video/mp4
Date:   Sun, 30 Jun 2013 22:12:30 GMT
Keep-Alive: timeout=10, max=50
Pragma: public
Server: Apache
content-transfer-encoding:  binary
这很难 - 大多数 php 配置将在 30 秒后失败。如果您拥有 php.ini,您可以将其更改为更长的限制。但仍然——这值得吗?我的意思是 - 文件可能会变大或网络变慢 - 并且您将再次遇到超时。
这就是为什么制作了下载器 - 以较小的块下载大文件Half Crazed向您展示了这个答案的代码(它不仅是一个 - 这仅考虑了客户协商传输的方式之一 - 但仍然是一个好的开始)。
例如,Mega.co.nz 使用新的 html5 功能。使用块在浏览器中下载文件,加入用户的文件,然后从浏览器磁盘空间下载它。它可以恢复文件、暂停文件等。(抱歉 - 没有代码,因为它会很大并且包含不止一种语言(php、js))。
PS:将你的改为  readfile($path);:
$handle=fopen($path, 'rb');
while (!feof($handle))
{
    echo fread($handle, 8192);
    flush();
}
fclose($handle);
这不会将整个文件加载到内存中,而只是一次加载 8KiB 的部分文件,然后将它们发送给用户。