如何让视频文件链接点击后下载而不是播放

江河/ 2023年06月15日/ PHP/ 浏览 1375

在大多数浏览器中,mp4等视频文件的链接,在点击后都会在浏览器中播放。如果希望点击后下载,该怎么作呢?


这个问题的根本原因在于,请求的头设置。只要把请求头设置为:Content-type: octet/stream。


比如,把链接写成下面的样子:


<a href="https://XX.mp4?response-content-type=application%2Foctet-stream">视频下载</a>


更好的方式是通过PHP代码设置:


$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;


然后,把链接写成下面的样子就可以了。


<a href="direct_download.php?file=xxxx.mp4">视频下载</a>


发表评论

暂无评论,抢个沙发...

客服 工单