1

for my current project i need a folder that contains videos which should only be seeable for registered users, i know that i can block the access via .htaccess file but at least i dont exactly get what i want..

my currently .htaccess file is like this:

Deny from all 

at the moment im importing the videos/pictures through php which works for accessing them but its really really slow..

here is an example for my pictures:

<img src="data:image/png;base64,<?php echo base64_encode(file_get_contents("directory")) ?>">

and here for my videos:

<img src="data:video/mp4;base64,<?php echo base64_encode(file_get_contents("directory")) ?>">

im sure there is a better way where the page doesnt take forever to load, i also tried a video stream with php but i get another problem there, i cant leave the page anymore or again like before it takes forever to click links or leave.. :(

help is much appreciated!

Leander
  • 46
  • 5

2 Answers2

1

You can do something like <img src="getfile.php?file=foo.mp4"> and:

$file = validate_file_name_and_path($_GET['file']);

if( user_logged_in() && user_can_access($file) ) {
  // set appropriate headers, eg:
  header('Content-Type: video/mp4');
  readfile($file);
  exit();
}
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • yes im doing something similar like this but i still have the same problem with the extremly long waiting times – Leander Apr 29 '23 at 22:33
  • No, what you've posted embeds the files directly in the page source. This will fetch them in a separate request. This lets the page render far faster, but the total time required to download the video file data will not change because you still have to download it one way or another. If you want this to go faster, then you need a faster connection and/or disk I/O. – Sammitch Apr 29 '23 at 23:00
  • ok thanks, do you maybe know why it doesnt work on mobile devices? – Leander Apr 29 '23 at 23:08
1

For anyone having the same issue i found a working code in another thread: https://stackoverflow.com/a/47626870/16587767

the code which also works on mobile devices while still having the skip functionality in the videos is this:

$file='path/file.mp4';

header('Content-Type: video/mp4'); #Optional if you'll only load it from other pages
header('Accept-Ranges: bytes');
header('Content-Length:'.filesize($file));

readfile($file);

Leander
  • 46
  • 5