Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play mp4 file through php in HTML5 Video Tag in Chrome?

Tags:

php

video

mp4

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,

like image 655
vnoo Avatar asked Oct 17 '25 08:10

vnoo


1 Answers

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;
}
like image 143
vnoo Avatar answered Oct 19 '25 23:10

vnoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!