[英]How to serve a video from a file outside the web root in PHP to a html5 video source
I currently have a file called index.php
in my web root folder /var/www/html/
which should load an image and a video in a directory outside of the web root.我目前在我的 web 根文件夹/var/www/html/
中有一个名为index.php
文件,它应该在 web 根目录之外的目录中加载图像和视频。
The image and a video file is located outside the web root folder as follows: /var/www/media/image.jpg
and /var/www/media/movie.mp4
.图像和视频文件位于 Web 根文件夹之外,如下所示: /var/www/media/image.jpg
和/var/www/media/movie.mp4
。
To access the image file, I have created a php file within the web root folder called serve_image.php
with the following contents:为了访问图像文件,我在名为serve_image.php
的 web 根文件夹中创建了一个 php 文件,其中包含以下内容:
<?php
header('Content-Type: image/jpg');
readfile("../media/image.jpg");
?>
then, within index.php, i load this image into an <img>
element as follows:然后,在 index.php 中,我将此图像加载到<img>
元素中,如下所示:
<html>
<body>
<img width="320" height="240" src="serve_image.php"/>
</body>
</html>
and the image is correctly displayed.并且图像正确显示。
If I try to do the same thing with the mp4 file, it does not show the video.如果我尝试对 mp4 文件做同样的事情,它不会显示视频。 I have created a php file within the web root folder serve_movie.php
with the following contents:我在 web 根文件夹serve_movie.php
创建了一个 php 文件,内容如下:
<?php
header('Content-Type: video/mp4');
readfile("../media/movie.mp4");
?>
then, within index.php, i load try to load this movie into an <source>
element within a <video>
element as follows:然后,在 index.php 中,我尝试将这部电影加载到<video>
元素中的<source>
元素中,如下所示:
<html>
<body>
<video width="320" height="240" controls autoplay>
<source src="serve_movie.php" type="video/mp4"/>
</video>
</body>
</html>
However, no video is shown.但是,没有显示视频。
What do I need to do to correctly load the video?我需要做什么才能正确加载视频?
Your code looks correct, and when I've just tested a very similar script, it has worked perfectly.您的代码看起来是正确的,当我刚刚测试了一个非常相似的脚本时,它运行良好。
A couple of thoughts:一些想法:
To enable error reporting, add the following to your video script (serve_movie.php) before any other code:要启用错误报告,请在任何其他代码之前将以下内容添加到您的视频脚本 (serve_movie.php) 中:
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.