如何设置URL直接可下载

时间:2018-11-15 10:09:13

标签: php html web hosting cpanel

我有一个简短的视频共享网站。用户可以从那里下载视频。我现在遇到了一些问题。我希望用户直接下载视频,但是当用户访问我的链接浏览器时,会自动播放视频。

我只希望在用户单击链接时自动开始视频下载。

现在我正在分享这样的视频链接   例如:www.example.com/examle.mp4

请给我建议

3 个答案:

答案 0 :(得分:1)

谢谢你们的回答。我希望我的URL可以直接下载。你们建议我使用php文件,但我在网上找到了解决方案。这项工作。

我只需要将此代码放在.htaccess

<FilesMatch "\.(mp4|MP4)">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

这很好用。

谢谢你们

答案 1 :(得分:0)

<?php
$file = "www.example.com/examle.mp4"; 

header("Content-Description: File Transfer"); 
header("Content-Type: application/octet-stream"); 
header("Content-Disposition: attachment; filename='" . basename($file) . "'"); 

readfile ($file);
exit(); 
?>

答案 2 :(得分:0)

您可以链接到php文件,而不是直接链接到视频文件,该文件确实会触发下载。

<?php
  header('Content-disposition: attachment; filename=movie.mp4;');
  header('Content-Length: '. filesize('movie.mp4'));
  readfile('movie.mp4');
  exit;

使用.htaccess,您可以将mp4链接重定向到您的php文件。

RewriteEngine on
RewriteRule ^movie.mp4$ ./movie.php