【问题标题】:How to force browser to download HUGE mp4 file instead of playing it如何强制浏览器下载巨大的 mp4 文件而不是播放它
【发布时间】:2019-09-23 20:51:23
【问题描述】:

好的,我知道这听起来像是重复的,但事实并非如此。 有很多关于下载 mp4 文件而不是通过浏览器播放的问题和答案。

我的问题是我的 mp4 大小为 1GB,而我的服务器有 512 个内存和 1 个 CPU,所以这种方法对我不起作用。

这是我当前的代码:

<?php
ini_set('memory_limit', '-1');
$file = $_GET['target'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

有没有办法让它在一个大文件上发生?

【问题讨论】:

  • 在.htaccess 中使用Header 指令设置标题并直接链接文件怎么样?
  • @MichalHynčica 我只是在学习人:D
  • 我没有尝试过,所以我只在 cmets 中建议它。但我相信如果使用 php 的唯一原因是设置 headers,最好用mod_headers 设置它们并让 apache 处理发送文件而不是在 php 中打开它。
  • 另外,mime 类型应该是application/octet-stream 而不是八位字节/流。

标签: php server download


【解决方案1】:

您是否尝试过分块下载文件,例如:

$chunk = 256 * 256; // you can play with this - I usually use 1024 * 1024;
$handle = fopen($file, 'rb');
while (!feof($handle))
{
    $data = fread($handle, $chunk);
    echo $data;
    ob_flush();
    flush();
}
fclose($handle);

【讨论】:

    猜你喜欢
    • 2015-06-30
    • 2014-05-22
    • 2013-02-21
    • 2011-06-19
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多