AvinashK
AvinashK

Reputation: 3423

video from filesystem in html page

I am writing a PHP code where I display few videos in my external hard disk, on an html page. Apache has access to the folder where the videos are present.

The PHP code is as follows:-

<html>

<?php
foreach(glob('G:/path/to/videos/*') as $file)
{
        echo $file,"\n";   // --------------> This line works and displays the video names
}
?>

<video width="320" height="240" controls>
  <source src="G:/path/to/videos/video 1.mp4" type="video/mp4">  ----> PROBLEM lies here
</video>

</html>

However, the video player which is displayed on the HTML page in browser doesn't play any video.

Can anyone tell me what am I doing wrong?

Upvotes: 1

Views: 645

Answers (2)

Shameerariff
Shameerariff

Reputation: 165

You have the problem in accessing the video files. Here the best solution is move all the files to the separate folder like videos in web server (WAMP / LAMP) location and point these files as follows.

<html>


<video width="320" height="240" controls>

  <source src="videos/video1.mp4" type="video/mp4">  ----> PROBLEM lies here

</video>

</html>

Upvotes: 1

theBurlyone
theBurlyone

Reputation: 19

You can store the actual filepath in your database and request it with php. You can then copy the file to a temporary folder for the site and pass that file path back to the front-end, then you trigger a delete function once they close the file. So for instance I used the following code in my app, first I send an ajax request to a php file with the variable "filepath": copy(DOCUMENT_ROOT."/".$_REQUEST['filepath'].".pdf",SITE_ROOT.TEMP_STORAGE."/".$_REQUEST['filepath'].".pdf");

So I defined a DOCUMENT_ROOT in my config.php file for the actual base location of all files. I also specified a SITE_ROOT and TEMP_STORAGE.  SITE_ROOT being the basic server file path to the site, and TEMP_STORAGE being the default temporary location to store files for viewing.
I then do the following: $outcome = array("location" => SECURE_ROOT.TEMP_STORAGE."/".$_REQUEST['filepath'].".pdf", "actual_path" => SITE_ROOT.TEMP_STORAGE."/".$_REQUEST['filepath'].".pdf");

So I pass both the server location of the file and the actual URL to access the file.


You then access the location variable from your ajax success function, and when you are done you call a delete function and pass the actual path back to a php function and use the following code:

if(!unlink($_REQUEST['filepath'])){
   $response = "unable to delete file.";
}else{
   $response = "file deleted successfully.";
}

Not sure if this is the best method, but it does work on a commercial app I developed. And it hides the actual server file path from public eyes.

After reading my own answer it seems confusing, haha. So if you want further clarification, please let me know.

Upvotes: 1

Related Questions