0

I want to create a movie streaming website at home so that it can be reached via a web browser anywhere within the house. 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?

Warosaurus
  • 520
  • 1
  • 5
  • 14
  • I don't want to distract you from what would obviously be a fun project, but have you come across things like [Plex](https://plex.tv/) before? – Clive Sep 24 '14 at 17:40
  • 1
    You would probably need to either setup an alias in apache to point the remote directory to a folder under the document root or simply create a php script that lies within the document root and just sends out the right headers and calls `readfile($src);`. – Jonathan Kuhn Sep 24 '14 at 17:40
  • @Clive, thanks I have heard about Plex, but I would like to make this on my own as a project like you suggest. :) – Warosaurus Sep 24 '14 at 17:49

1 Answers1

2

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
  2. One for the file in the src attribute

For that second request, you need to make another PHP "page". (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. 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"]. Then it just needs to stream that file. I could go into the details on that part, but it's already been answered elsewhere.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    I just want to say to be extremely careful with something like this. You are essentially allowing access to any file on your system, including sensitive files and php sources. There needs to be some sort of protection here to prevent files outside of your movie directory from being loaded. – Jonathan Kuhn Sep 24 '14 at 18:00