I have a problem with mp4 files. Html5 Video Tag can play it with direct link, but not with PHP Header (Mp3 is worked fine with PHP Header). I have tried almost of solution in Stack but still not resolve my problem :(
Hear is my code:
PHP mp4.php
error_reporting(0);
$local_file = 'z.mp4';//'b.mp3';
$download_file = 'out.mp4';
// send headers
header('Cache-control: private');
header('Content-Type: video/mp4');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);
header('Accept-Ranges: bytes');
header("Content-Transfer-Encoding: binary");
readfile($local_file);
HTML
<video controls="" autoplay="" name="media">
<source src="http://localhost/videos/mp4.php" type="video/mp4">
</video>
I don't know why :( Please help me.
Thanks,
OK! I've resolved my problem :) Hope this will help anyone has the same problem as me
Just use that code:
$file = 'local_file';
$newfile = 'outFile';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($newfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
I think you've added unnecessary headers to your php file. Try this one:
$local_file = 'z.mp4';
$size = filesize($local_file);
header("Content-Type: video/mp4");
header("Content-Length: ".$size);
readfile($local_file);
And a HTML code something like this
<video width="480" height="320" controls>
<source src="mp4.php" type="video/mp4">
</video>
EDIT: This will also work for mp3 files when you change Content-Type header and html video type.
<video width="480" height="320" controls>
<source src="mp3.php" type="audio/mpeg">
</video>