【问题标题】:Force download mp4 files强制下载 mp4 文件
【发布时间】:2012-12-31 05:52:50
【问题描述】:

我想强制用户下载 youtube 视频。 例如this 网址。我下载了视频,我可以播放原始视频,但是即使视频的长度/大小相同,我在 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);

【问题讨论】:

    标签: php youtube file-get-contents readfile


    【解决方案1】:

    如果你可以使用 htaccess...

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

    我也使用它来强制下载 PDF。效果很好!

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

    How to force file download with PHP

    编辑:hrmm...但是您已经做了类似的事情,但不起作用...看看您是否可以让 htaccess 与您正在尝试做的事情一起工作。

    【讨论】:

    • 可能用户没有使用 Apache!
    • 因此前言“如果你可以使用 htaccess...”
    • +1 这比通过 PHP 解释器运行巨大的视频文件要好得多。
    • 对不起@Eric,我在你编辑答案之前写了评论。
    • @Ako 嘿,没问题,我知道提问者正在寻找 PHP 解决方案,只是想我会把 htaccess 扔出去,希望它能坚持下去。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多