title: Streaming video/mp4 with php
body: <p>I've got problem streaming a mp4 file throught php.
  The downloading of the files hangs on 1MB. You can see it here: 
  <img src="https://i.stack.imgur.com/NGHVm.png">
  I've tried many headers and read many threads, but lots of them are unresolved or didnt help me, like this one: <a href="https://stackoverflow.com/questions/3128906/mp4-plays-when-accessed-directly-but-not-when-read-through-php-on-ios">MP4 plays when accessed directly, but not when read through PHP, on iOS</a> . Any help please?</p>
  
  <p>There is my code:</p>
  
  <pre><code>&lt;?
  $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 &gt; $buffer){
          echo fread($data, $buffer);
          flush();
      }else{
          echo fread($data, $bytesleft);
          flush();
      }
      //sleep(1);  // Speedlimit = one buffer per second
  }
  
  
  fclose($data);
  exit;
  ?&gt;
  </code></pre>
  
  <p>Thanks in advance</p>