Php 如何强制浏览器下载巨大的mp4文件而不是播放它

Php 如何强制浏览器下载巨大的mp4文件而不是播放它,php,server,download,Php,Server,Download,好吧,我知道这听起来像是重复的,但不是。 关于下载mp4文件而不是通过浏览器播放它,有很多问题和答案 我的问题是我的mp4有1GB大小,我的服务器有512个ram和1个CPU,所以,这种方法对我不起作用 这是我当前的代码: <?php ini_set('memory_limit', '-1'); $file = $_GET['target']; header ("Content-type: octet/stream"); header ("Content-disposition: atta

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

我的问题是我的mp4有1GB大小,我的服务器有512个ram和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我只是在学习,男士:DI没有尝试过,这就是为什么我只在评论中建议它。但我认为,如果使用php的唯一原因是设置头,那么最好使用设置头,让apache处理发送文件,而不是用php打开文件。mime类型应该是
application/octet-stream
而不是octet/stream。
$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);