0

I have a playable .mp4 file and I used this code to download the file using PHP.

Web browser downloads file ok, but the mp4 file is not playable after downloading.

Does the code have any issue?

<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT'] . "/video/";
$fullPath = $path."test_video.mp4";

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
?>
techraf
  • 64,883
  • 27
  • 193
  • 198
jeon
  • 123
  • 1
  • 3
  • 15

2 Answers2

0

I don't have the repotations to comment. Please check your code for other files such as text or image.I think you must be getting an error there too.

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
0

First header("Content-type: application/octet-stream"); is not needed to download file. Source

Second

change your header with,

header('Content-type: video/mp4');
header('Content-type: video/mpeg');

this should work.

Community
  • 1
  • 1
Archish
  • 850
  • 8
  • 32
  • If I changed it to video/mp4 then it shows the video on the web browser instead of downloading it. – jeon Sep 07 '16 at 23:07
  • 1
    You can force your browser to download in the headers. See the answer here: http://stackoverflow.com/questions/21091766/mp4-download-causes-browser-to-play-file-instead-of-download – Michael S Sep 07 '16 at 23:16