2013-01-16 234 views
1

我想强制用户下载YouTube视频。 例如this url。我下载视频,我可以播放原始视频,但即使是视频的长度/大小也是一样的,我不能在force_downloaded时播放它。强制下载mp4文件

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

回答

8

如果你可以使用htaccess的...

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

我用这个来迫使PDF文件下载为好。很棒!

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

How to force file download with PHP

编辑:人力资源管理模式...但你已经做了哪些工作不...看看你是否能得到htaccess的与你正在试图做我猜的工作类似的东西。

+1

也许用户没有使用Apache! –

+0

因此序言“如果您可以使用htaccess ...” – Eric

+3

+1这比通过PHP解释器运行巨大的视频文件要好得多。 –