title: Cakephp video passthrough
body: <p>I have been developing a website for teachers using cakephp 2.0.3. One requirement is for the teachers to be able to upload files (images, docs, pdfs and videos) that are only viewable by a students in a given class. I had been using a php fpassthrough to read binary data to the http output stream. </p>
  
  <p>This has worked fine for all the documents except for the video, which won't be played by the chrome built-in player, vlc firefox plugin or jwplayer, which will be used on the actual web site. If I save the file, however, Windows media player will play the file fine. Playing the file from the cakephp webroot directory also works fine. Here is the file view code:</p>
  
  <p>The controller code, which loads and outputs the file:</p>
  
  <pre><code>//cakephp find used to pull file information from database
  $fileName = "/redacted/" . $File['File']['unit_id'] . "/" . $File['File']['id'];
  $fp = fopen($fileName, 'rb');
  
  header("Content-Type: " . $File['File']['type']);
  header("Content-Length: " . filesize($fileName));
  
  fpassthru($fp);
  </code></pre>
  
  <p>The layout which is used:</p>
  
  <pre><code>&lt;?php echo $this-&gt;fetch('content'); ?&gt;
  </code></pre>
  
  <p>The jwplayer loading code:</p>
  
  <pre><code>&lt;div style="width: 200px; height: 91px;" id="1"&gt;&amp;nbsp;&lt;/div&gt;
  &lt;script type="text/javascript"&gt;
      jwplayer("1").setup({
          file: "/Files/view/1/blah.m4v",
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>The view file is empty, as all output is done in the controller. I've looked through this site and google but can't find a reason for the issue or better way to perform this function. I have verified <a href="https://stackoverflow.com/questions/3222642/why-is-php-interfering-with-my-html5-mp4-video">that the headers are correct and that there are no errant line breaks or data before or after</a>. I have also checked that the extension is not required (in the final code block, '1' specifies the file ID, and the file name at the end is a dummy).</p>
  
  <p>My questions, therefor:</p>
  
  <ol>
  <li>Is fpassthru(), readfile() or similar the correct method for php video passthrough?</li>
  <li>Is this problem specific to my code, or inherent to cakephp's handling of MVC? Has anyone successfully implemented such a solution?</li>
  <li>Are there any other 'thinking outside the box' solutions (using node.js or some other language, .htaccess trickery) that would work better. </li>
  <li>I could use a non-cakephp script in the webroot folder, but how would I go about passing user session data? Is this just a dumb idea?</li>
  </ol>
  
  <p><strong>[Edit]</strong>
  After testing fpassthrough on video files separate from cakephp, I found that the issue is with the video passthrough rather than cakephp. This code was used to test, in the app/webroot directory:</p>
  
  <pre><code>&lt;?php
      $fileName = "/var/www/redacted/files/2/1";
      $fp = fopen($fileName, 'rb');
      header("Content-Type: video/mp4");
      header("Content-Length: 18485");
      fpassthru($fp);
  ?&gt;
  </code></pre>
  
  <p>The content type, path, and length were taken from the database entry for that file.</p>