Given your comment on making something similar to a Netflix clone, my suggested advice would be to make a PHP endpoint that exports the file list as JSON. That being said, I do not suggest you build a file browser that allows user input as this will, in many cases, open you up to directory traversal attacks and a malicious user may be able to gain access to your entire server. It can be done properly, but is tricky.
Instead, why not have a database that contains file names, types, paths, keywords, thumbnail image paths, etc. Then use client side javascript to make requests via ajax to a PHP script that handles the database access and fully controls the file access. In this way, when a user clicks on a video, you can then call another endpoint that initiates a websocket for video streaming and the user cannot maliciously wander around in your server or download/steal your video files directly.
Here is an example of listing some files from your server. For code simplicity, I am assuming jQuery here, but you can use another library or native XMLHttpRequest object:
$.ajax({
url: '/movies/list-all.php',
method: 'GET',
dataType: 'json'
}).done(function(data) {
if (typeof data === 'object' && typeof data !== null) {
// List the movies
for(var movie in data.movies) {
var movieDiv = '<div class="movie-item" data-id="' + movie.id + '">' +
'<img src="' + movie.thumbnail_path + '"><br>' +
'<p>' + movie.title + '</p>' +
'</div>';
$('#my-container-div').append(movieDiv);
}
}
});
Example PHP endpoint to list movies:
<?php
/**
* list-all.php
*
* Assumes you have a PDO database connection setup already here as $db.
*/
$stmt = $db->query("SELECT * FROM movies");
if ($stmt instanceof PDOStatement) {
$movies = $stmt->fetchAll();
$response = (object) [
'total' => count($movies),
'movies' => $movies,
];
} else {
$response = (object) [
'total' => 0,
'movies' => [],
]
}
header('Content-Type: application/json');
echo(json_encode($response));
Demo (sort of):
var movieData = {
count: 3,
movies: [{
id: 1,
title: "Movie A",
thumb: "https://via.placeholder.com/150"
},
{
id: 2,
title: "Movie B",
thumb: "https://via.placeholder.com/150"
},
{
id: 3,
title: "Movie C",
thumb: "https://via.placeholder.com/150"
},
]
};
$(document).ready(function() {
if (typeof movieData === 'object' && typeof movieData !== null) {
// List the movies
for (var i in movieData.movies) {
var movie = movieData.movies[i];
var movieDiv = '<div class="movie-item" data-id="' + movie.id + '">' +
'<img src="' + movie.thumb + '"><br>' +
'<p>' + movie.title + '</p>' +
'</div>';
$('#movies').append(movieDiv);
}
}
});
.movie-item {
display: inline-block;
border: 2px solid #DDD;
margin: 10px;
text-align: center;
}
.movie-item:hover {
border: 2px solid #00aaca;
cursor: pointer;
}
.movie-item p {
color: #00aaca;
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head></head>
<body>
<h1>Movies</h1>
<div id="movies"></div>
</body>
</html>
give the choice to the local user to clic on any of them-- what is the intended purpose? What is supposed to happen once they click the file name?