尝试使用php仅向经过身份验证的用户流式传输视频
我正在尝试使用php仅向经过身份验证的用户流式传输视频 下面是我正在尝试的代码,但无法播放mp4视频 index.php尝试使用php仅向经过身份验证的用户流式传输视频,php,html,Php,Html,我正在尝试使用php仅向经过身份验证的用户流式传输视频 下面是我正在尝试的代码,但无法播放mp4视频 index.php <video controls="" autoplay="" name="media"> <source src="http://localhost/video_test/mp4.php" type="video/mp4"> </video> $newfile = '/var/www/html/v
<video controls="" autoplay="" name="media">
<source src="http://localhost/video_test/mp4.php" type="video/mp4">
</video>
$newfile = '/var/www/html/video_test/videos/MyVideo.mp4';
if (file_exists($newfile)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($newfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($newfile));
ob_clean();
flush();
readfile($newfile);
exit;
}
?>
我已经在做一个类似的项目,所以我抓取了一些代码,去掉了除基本内容之外的所有内容:- 这些视频被命名为000000.mp4~999999.mp4,暂时放在与PHP代码相同的目录中(我们稍后将移动它们) 这是播放视频的页面:-
<HTML>
<BODY>
<CENTER>
<?php // video-play.php
$vid = (int) $_GET['vid']; // Get video ID
echo "<VIDEO HEIGHT='95%' SRC='video-file.php?vid=$vid' CONTROLS='true' AUTOPLAY='true'>\n</VIDEO>\n";
?>
</CENTER>
</BODY>
</HTML>
我们之所以使用video-file.php,是因为从另一个角度看,只需进入浏览器即可播放视频,而不需要任何控件
当这项工作完成后,您需要决定如何控制访问。最简单的方法可能是通过会话
希望这有帮助你为什么要做ob_clean()?@PhilippGrassl甚至删除了ob_clean(),但仍然没有播放mp4视频。你可能想删除标题('Content-Disposition:attachment;filename='。basename($newfile));因为这只有在下载时才有用。当你在浏览器中打开mp4.php时,你看到的是mp4文件还是出现错误?视频位于公共可读区域,任何人都可以查看源代码、获取URL并播放。
<?php // video-file.php
$vid = (int) $_GET['vid']; // Get video ID
$vfn = sprintf("%06d.mp4",$vid); // Get video file from ID
$ok = true;
// Code to check if user can view video goes here
if ($ok)
{
header("Content-Type: video/mp4");
header('Pragma: public');
header('Content-Length: '.filesize($vfn));
readfile($vfn);
}
else
{
echo "<HTML>\n<BODY>\n";
echo "<CENTER>\n";
echo "<H2> Access Denied ! </H2>";
echo "</CENTER>\n";
echo "</BODY>\n</HTML>\n";
}
?>
$vfn = sprintf("../videos/%06d.mp4",$vid); // Get video file from ID