I have a video file which I need to grab using a function instead of a direct link.
When I view the video page code of the direct link, this is what I have on screen:

But when I use my function, first of all the video wont run, and when I view the code, this appears:

I realized it’s the same file, the same characters, but with many \0 characters in the middle. These are the one’s in yellow.
This is the code I’m using:
$file_name = --path to file--;
$file_size = filesize($file_name);
$ctype = 'video/mp4';
header('Content-Type: '.$ctype);
header('Content-Length: ' . $file_size);
header("Accept-Ranges: bytes");
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
$file = fopen($file_name, 'rb');
$read = fread($file, $file_size);
print($read);
fclose($file);
exit;
Do I need to change the code? Is there something to make fread read the file properly?
Or do I need to do some kind of processing to the file to be displayed?
Try using
readfile()instead offopen()/fread()/fclose(). It will be more memory efficient and less susceptible to errors.Having said that there’s no real reason why your code shouldn’t work (assuming that the file path is correct) – you should (for once) ensure that
display_errorsis off in case a PHP error is corrupting the file. You can do this by settingini_set('display_errors', 0);at the top of your script.