【发布时间】:2013-06-26 18:07:39
【问题描述】:
我正在尝试下载 .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
【问题讨论】:
-
您可能遇到了执行时间限制。下载是否总是在相同的时间段后停止?
-
可恢复文件下载需要更多的代码;所以你应该删除
Accept-Ranges。见:stackoverflow.com/questions/157318/… -
你真的在使用输出缓冲吗?如果不是,您可能不需要 ob_clean() 和 flush(),因为 readfile 命令会将数据直接输出到客户端。
-
我还没计时-@MikeW |它不可恢复,可以一次下载为一个文件。
-
@dethtron5000 即使我没有使用输出缓冲,我也很难让下载可靠地工作,除非我先清除缓冲区。这是一种一直对我有用的技术。
标签: php http-headers force-download