我正在尝试使用 php 将视频仅传输给经过身份验证的用户。
下面是我正在尝试的代码,但无法播放 mp4 视频。
index.php
<video controls="" autoplay="" name="media">
<source src="http://localhost/video_test/mp4.php" type="video/mp4">
</video>
mp4.php
$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-play.php?vid=1 播放视频 000001.mp4
这是获取视频文件并将其发送到浏览器的位:-
<?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";
}
?>
完成此操作后,您可以将视频移动到无法从 Internet 读取的目录(例如/var/www/videos )并将 video-file.php 中的第 3 行更改为:-
$vfn = sprintf("../videos/%06d.mp4",$vid); // Get video file from ID
我们使用 video-file.php 的原因是因为否则只需输入 http://your-domain.com/000001.mp4 即可播放视频。进入您的浏览器 - 绕过任何控件。
当您完成这项工作后,您需要决定如何控制访问。最简单的方法可能是通过 session 。
希望这有帮助
关于php - 尝试使用 php 将视频仅传输给经过身份验证的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32183980/