我试图下载一个.mp4文件。 (大约1.3GB大小)。 我使用的是以下几点: 强制下载,从PHP文件
<?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
您可能会遇到执行时间限制。下载总是在相同的时间段后停止吗? – 2013-06-28 21:08:46
可恢复的文件下载需要比这更多的代码;所以你应该删除'Accept-Ranges'。请参阅:http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file –
你实际上使用输出缓冲?如果不是,你可能不需要ob_clean()和flush(),因为readfile命令会直接将数据输出到客户端。 – dethtron5000