php - 如何强制浏览器下载巨大的 mp4 文件而不是播放它
问题描述
好的,我知道这听起来像是重复的,但事实并非如此。有很多关于下载 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;
?>
有没有办法让它发生在一个巨大的文件上?
解决方案
您是否尝试过分块下载文件,例如:
$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);
推荐阅读
- android - 如何从 CallScreeningService 中的 Call.Details 中提取电话号码?
- python - 如何正确转换列表的输出值以将它们用作函数内的 args?
- c# - 如何仅使用键盘通过 shell 注册表编辑器来控制我的 wpf 应用程序
- node.js - 如何让 heroku 使用 DB 的环境配置
- google-sheets - 用于过滤 Google 表格中具有最高值的唯一行的公式
- c# - Unity Build 错误:当前上下文中不存在名称“EditorUtility”
- javascript - 动态 ES2015 import() 外部模块(来自 Chrome 75 DevTools 的示例)
- list - 方案 - 附加到列表的末尾
- c# - 我无法让我的 odata 查询来计算嵌套元素
- java - Google Cloud SQL - 无法连接到 MySQL 数据库