强制下载mp4文件 [英] Force download mp4 files

查看:116
本文介绍了强制下载mp4文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强迫用户下载youtube视频. 例如网址.我下载了视频,然后可以播放原始视频,但是即使视频的长度/大小也一样,我在强制下载后无法播放.

I want to force the user tho download a youtube video. For example this url. I download the video and I can play the original video, but even the length/size of video is the same I cant play it when is force_downloaded.

function force_download($file,$video_url)
{

  $video_data = file_get_contents($video_url);
  file_put_contents($file, $video_data);

  if(isset($file) && file_exists($file))
  {
    header('Content-length: ' .  filesize($file));
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename= "' . $file . '"');

    readfile($file);
  }
}

force_download('youtube.mp4',$video_url);

推荐答案

如果可以使用htaccess ...

If you can use htaccess...

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

我也用它来强制PDF下载.很棒!

I use this to force PDFs to download as well. Works great!

这是另一个关于用PHP强制下载的堆栈问题...与我上面编写的htaccess基本上具有相同的作用.将内容配置设置为附件是关键.

Here is another stack question regarding forcing download with PHP... Does essentially the same thing as the htaccess I have written above. Setting the content-disposition to attachement is key.

如何使用PHP强制下载文件

hrmm ...,但是您已经做过类似的操作而无法正常工作...看看您是否可以让htaccess与您要尝试执行的操作一起工作.

hrmm... but you have already done something similar which isn't working... See if you can get the htaccess to work with what you're trying to do I guess.

这篇关于强制下载mp4文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆