Ben*_*Ben 2 php youtube file-get-contents readfile
我想强迫用户下载YouTube视频.例如这个网址.我下载视频,我可以播放原始视频,但即使是视频的长度/大小也是相同的,我在强制下载时无法播放.
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);
Run Code Online (Sandbox Code Playgroud)
如果你可以使用htaccess ......
<FilesMatch "\.(mp4|MP4)">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Run Code Online (Sandbox Code Playgroud)
我用它来强制下载PDF.效果很好!
这是关于用PHP强制下载的另一个堆栈问题...与我上面写的htaccess基本相同.将内容处置设置为附件是关键.
编辑:hrmm ...但你已经做了类似的东西不能正常工作......看看你是否可以让htaccess与我想要做的事情一起工作.