title: Stream mp4 video in php file with speed limitation
body: <p>I want to open a <strong>video/mp4</strong> file in a php file to use it in the player. I want to limit the file upload speed to <strong>100 kb/s</strong>.</p>
  
  <pre><code>&lt;?php
  $file = 'video.mp4';
  $fp = @fopen($file, 'rb');
  
  $size   = filesize($file); // File size
  $length = $size;           // Content length
  $start  = 0;               // Start byte
  $end    = $size - 1;       // End byte
  
  header('Content-type: video/mp4');
  header("Accept-Ranges: 0-$length");
  if (isset($_SERVER['HTTP_RANGE'])) {
  
      $c_start = $start;
      $c_end   = $end;
  
      list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
      if (strpos($range, ',') !== false) {
          header('HTTP/1.1 416 Requested Range Not Satisfiable');
          header("Content-Range: bytes $start-$end/$size");
          exit;
      }
      if ($range == '-') {
          $c_start = $size - substr($range, 1);
      }else{
          $range  = explode('-', $range);
          $c_start = $range[0];
          $c_end   = (isset($range[1]) &amp;&amp; is_numeric($range[1])) ? $range[1] : $size;
      }
      $c_end = ($c_end &gt; $end) ? $end : $c_end;
      if ($c_start &gt; $c_end || $c_start &gt; $size - 1 || $c_end &gt;= $size) {
          header('HTTP/1.1 416 Requested Range Not Satisfiable');
          header("Content-Range: bytes $start-$end/$size");
          exit;
      }
      $start  = $c_start;
      $end    = $c_end;
      $length = $end - $start + 1;
      fseek($fp, $start);
      header('HTTP/1.1 206 Partial Content');
  }
  header("Content-Range: bytes $start-$end/$size");
  header("Content-Length: ".$length);
  
  
  $buffer = 1024 * 8;
  while(!feof($fp) &amp;&amp; ($p = ftell($fp)) &lt;= $end) {
  
      if ($p + $buffer &gt; $end) {
          $buffer = $end - $p + 1;
      }
      set_time_limit(0);
      echo fread($fp, $buffer);
      flush();
  }
  
  fclose($fp);
  exit();
  ?&gt;
  </code></pre>
  
  <p>And it works fine, how to limit the speed of loading a file? And what are the optimal settings (so that the video loads smoothly, at a speed without jamming)</p>