3

I've got problem streaming a mp4 file throught php. The downloading of the files hangs on 1MB. You can see it here: I've tried many headers and read many threads, but lots of them are unresolved or didnt help me, like this one: MP4 plays when accessed directly, but not when read through PHP, on iOS . Any help please?

There is my code:

<?
$root=getenv("DOCUMENT_ROOT");
$file = "$root/".$_GET['get'];
header("Content-Type: video/mp4");
//header("Content-Type: application/octet-stream"); // downloads the file

$start=0;
$size = filesize($file);
$length = $size;
$end = $size - 1;

//header('HTTP/1.1 206 Partial Content');
header("Accept-Ranges: $start-$end");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");


$data = fopen("$file", "r");
fseek($data, $start);

$bytesleft = $size-$start + 1;
$buffer = 1024 * 256; // 256kb
while(!feof($data)){
    if($bytesleft > $buffer){
        echo fread($data, $buffer);
        flush();
    }else{
        echo fread($data, $bytesleft);
        flush();
    }
    //sleep(1);  // Speedlimit = one buffer per second
}


fclose($data);
exit;
?>

Thanks in advance

Community
  • 1
  • 1
  • 2
    This code is extraorinarily dangerous. consider `example.com?code.php?id=../../../../../../../../etc/passwd`. You're allowing users to grab **ANY** file on your server. – Marc B Jan 05 '15 at 14:10
  • ah, I see. Ok thanks, Ill make security after I get this to work. Any ideas, what is wrong? – Marek Schubert Jan 05 '15 at 14:18
  • 1
    can't really see anything "wrong". lots of symtoms of cargo-cult programming, and I don't know why you're bothering with that big `bytesleft > buffer` business - fread can't go past the end of the file, and you never update `$bytesleft`. – Marc B Jan 05 '15 at 14:22
  • ok so ill delete the $bytesleft etc. But the code is not copied :) – Marek Schubert Jan 05 '15 at 14:26
  • From the PHP manual pages: flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those. -- Also, check to make sure you don't run into any script timeouts. Also, $start-$end is negative, there's no point in having a content "range" if you only have 1 chunk. Overall do a bit more research before you attempt this. – apokryfos Jan 05 '15 at 14:27
  • header("Accept-Ranges: $start-$end"); $start-$end is not negative, because in header it does not mean minus. Output of Accept-Ranges header in some mp4 file is 0-228987440. And im not programming for first time ;) – Marek Schubert Jan 05 '15 at 14:37

1 Answers1

0

Nevermind. I downloaded the file from the server and compared it with original stored on server in hex. The downloaded one had one more byte on begining: 0A and that was the problem. Everything works now.