Force download mp4 files

限于喜欢 提交于 2020-01-14 02:45:40

问题


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);

回答1:


If you can use htaccess...

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

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

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.

How to force file download with PHP

EDIT: 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.



来源:https://stackoverflow.com/questions/14366424/force-download-mp4-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!