强制下载,来自php文件 [英] Force-downloading, from php file

查看:96
本文介绍了强制下载,来自php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载.mp4文件。 (约1.3GB大小)。
我正在使用以下内容:

I'm attempting to download a .mp4 file. (about 1.3GB size). I'm using following:

<?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)。

I open my php file, and firefox pops up with the "want to save" menu. Size looks right. I press Save As, to my desktop. The final downloaded file, lists as a random size, around 400MB (330, 463 and 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,你可以将其更改为更长的限制。但仍然 - 这甚至值得吗?我的意思是 - 文件可能变得更大或网络速度更慢 - 再一次你会达到超时。

THis is hard - most php configuration will fail after 30 seconds. If you own php.ini you can change that to longer limit. But still - is that even worth it? I mean - the files can get bigger or network slower - and once more you will hit the timeout.

这就是为什么下载程序的原因 - 以较小的块下载大文件 Half Crazed 向您展示了我的代码这个答案(它不仅仅是一个 - 这只考虑了客户协商转让的方式之一 - 但仍然是好的开始)。

This is why downloaders were made - to download big files in smaller chunks Half Crazed showed you code for that i THIS answer (its not only one - this only takes into account one of the ways clients negotiate the transfers - but still its a good start).

Mega.co.nz例如使用新的html5功能。使用块在浏览器中下载文件,在用户上加入文件,然后从浏览器磁盘空间下载它。它可以恢复文件,暂停文件等。 (对不起 - 没有代码,因为它会很大并且包含多种语言(php,js))。

Mega.co.nz for example uses new html5 features. Downloads the file in browser using chunks, joining the file on user and and then ,,downloading'' it from the browser disk space. It can resume files, pause files and so on. (Sorry - no code for that as it would be quite big and include more than one language (php, js)).

PS:改变你的 readfile($ path); into:

PS: change yours readfile($path); into:

$handle=fopen($path, 'rb');
while (!feof($handle))
{
    echo fread($handle, 8192);
    flush();
}
fclose($handle);

这不会将WHOLE文件一次加载到内存中,只加载8KiB的一部分,然后将它们发送给用户。

This will not load WHOLE file into memory, just parts of 8KiB at once and then send them to user.

这篇关于强制下载,来自php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆