简体   繁体   English

PHP家庭电影流

[英]PHP Home movie streaming

I want to create a movie streaming website at home so that it can be reached via a web browser anywhere within the house. 我想在家中创建一个电影流媒体网站,以便可以在房子内的任何地方通过Web浏览器访问它。 Right now I am stuck with the browser not being allowed to load the resource as it's located on another disk and not within the website site folder. 现在,我被限制在浏览器中,因为它位于另一个磁盘上而不是网站站点文件夹中,因此不允许加载该资源。

Here is the code: 这是代码:

<!DOCTYPE html>
<html>
<body>

<?php 
$type = "video/mp4";
$src = "E:/Movies/Movie.mp4";
if (file_exists($src)) { 
    echo "<p>File exists.";
    echo '<video>
              <source type="$type" src="$src">
          </video>';
    }?>
</body>
</html>

How would I enable loading the resource? 如何启用加载资源?

Consider logically what you're doing here. 从逻辑上考虑您在这里做什么。 While the server-side code can indeed see the file, the client-side browser of course can not. 尽管服务器端代码确实可以看到该文件,但客户端浏览器当然不能。 And the code currently relies on that by sending this to the client: 并且当前代码通过将其发送给客户端来依赖于此:

<source type="video/mp4" src="E:/Movies/Movie.mp4">

Instead of thinking about the file itself, think about the request that the browser needs to make for the file. 不用考虑文件本身,而考虑浏览器需要对该文件进行的请求。 The browser is actually making two requests here: 浏览器实际上在这里发出两个请求:

  1. One for the HTML that PHP outputs 一种用于PHP输出的HTML
  2. One for the file in the src attribute 一个用于src属性中的文件

For that second request, you need to make another PHP "page". 对于第二个请求,您需要创建另一个PHP“页面”。 (Though that "page" isn't going to return a web page, it's going to return the actual file.) That second page will need to know which file is being requested and will need two things: (尽管“页面”不会返回网页,但它将返回实际文件。)第二个页面将需要知道请求哪个文件,并且需要两件事:

  1. It needs to know which file the client wants 它需要知道客户想要的文件
  2. It needs to stream the file to the client 它需要将文件流式传输到客户端

For the first part, you can add the file name itself to the query string on the URL. 对于第一部分,您可以将文件名本身添加到URL上的查询字符串中。 You'll want to be careful here because clients can request any file , so in your server-side code you'll want to ensure that requests for unauthorized files are denied. 您将在这里非常小心,因为客户端可以请求任何文件 ,因此在服务器端代码中,您将要确保拒绝对未授权文件的请求。 But essentially the code might look something like this: 但是本质上,代码可能看起来像这样:

$src = "streamMovie.php?file=" . urlencode("E:/Movies/Movie.mp4");

This should result in something like: 这应该导致类似:

<source type="video/mp4" src="streamMovie.php?file=E%3A%2FMovies%2FMovie.mp4">

Now steamMovie.php knows which file is being requested by checking the value in $_GET["file"] . 现在steamMovie.php通过检查$_GET["file"]的值知道要请求哪个文件。 Then it just needs to stream that file. 然后,它只需要流式传输该文件。 I could go into the details on that part, but it's already been answered elsewhere . 可以在那部分进行详细介绍,但是其他地方已经回答了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM