title: Symfony2 video streaming
body: <p>I'm writing app in <code>symfony2</code> and I have a problem with video streaming. </p>
  
  <p>If one action takes a long time to execute -  for example <code>1 minute</code>, the whole app is frozen (when opened in second tab) and must wait for end of that execution. </p>
  
  <p>Where is the problem?  </p>
  
  <p>//EDIT</p>
  
  <pre><code>    public function streamAction($fileName) {
  
      $user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
      $request = $this-&gt;getRequest();
  
      $uid = $request-&gt;get('uid') != 'null' ? $user-&gt;getId() : $request-&gt;get('uid');
  
      $libPath = $this-&gt;_libPath('Users', 'uid' . str_pad($uid, 6, "0", STR_PAD_LEFT));
  
      $file = pathinfo($fileName);
      $fileName = $file['basename'];
      $fileExt = $file['extension'];
      $filePath = realpath($libPath . $fileName);
  
      if (in_array($fileExt, $this-&gt;formats['video'])) {
          $mime = 'video';
      }
  
      if (in_array($fileExt, $this-&gt;formats['audio'])) {
          $mime = 'audio';
      }
  
      $mime .= '/' . $fileExt;
  
      header("Accept-Ranges: bytes");
  
      if (is_file($filePath)) {
          header("Content-type: $mime");
          if (isset($_SERVER['HTTP_RANGE'])) {
  
              $fp = fopen($filePath, 'rb');
              $size = filesize($filePath);
              $length = $size;
              $start = 0;
              $end = $size - 1;
  
              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('ETag: "' . md5(microtime()) . '"');
              }
  
              header("Content-Range: bytes $start-$end/$size");
              header("Content-Length: $length");
              header('Connection: Close');
  
              $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);
          } else {
              header("Content-Length: " . filesize($filePath));
              readfile($filePath);
          }
      } else {
          echo "error";
      }
      die();
  }
  </code></pre>
  
  <p>Problem is not code because symfony2 block not only with streaming but with other long time actions like downloading files.</p>