5

我正在尝试下载 .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
4

2 回答 2

2

这很难 - 大多数 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 的一部分,然后将它们发送给用户。

于 2014-05-03T19:31:59.223 回答
0
   <?php
$filename = "theDownloadedFileIsCalledThis.mp4";
$myFile = "/absolute/path/to/my/file.mp4";

// Add bellow code for mime type
$ext=strtolower(substr($fl,strrpos($myFile,".")));
$mime_types = array(
            '.txt' => 'text/plain',
            '.htm' => 'text/html',
            '.html' => 'text/html',
            '.php' => 'text/html',
            '.css' => 'text/css',
            '.js' => 'application/javascript',
            '.json' => 'application/json',
            '.xml' => 'application/xml',
            '.swf' => 'application/x-shockwave-flash',
            '.flv' => 'video/x-flv',

            // images
            '.png' => 'image/png',
            '.jpe' => 'image/jpeg',
            '.jpeg' => 'image/jpeg',
            '.jpg' => 'image/jpeg',
            '.gif' => 'image/gif',
            '.bmp' => 'image/bmp',
            '.ico' => 'image/vnd.microsoft.icon',
            '.tiff' => 'image/tiff',
            '.tif' => 'image/tiff',
            '.svg' => 'image/svg+xml',
            '.svgz' => 'image/svg+xml',

            // video
            '.3gp' => 'video/3gpp',
            '.3g2' => 'video/3g2',
            '.avi' => 'video/avi',
            '.mp4' => 'video/mp4',
            '.asf' => 'video/asf',
            '.mov' => 'video/quicktime',
        );

if (array_key_exists($ext, $mime_types)){
   $mm_type=$mime_types[$ext];
}else{
   $mm_type="application/octet-stream";
}
$mm_type="application/octet-stream";

header("Cache-Control: public, must-revalidate"); // Avoid this line
header("Pragma: public"); // Add this line
header("Pragma: hack"); // Avoid this line
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($myFile)) ); // Avoid this line
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: ' . filesize($myFile)); // Add this line
header("Content-Transfer-Encoding: binary\n");
ob_clean(); // Add this line

readfile($myFile);

?>
于 2013-06-30T18:41:00.537 回答