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.
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:
The controller code, which loads and outputs the file:
//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);
The layout which is used:
<?php echo $this->fetch('content'); ?>
The jwplayer loading code:
<div style="width: 200px; height: 91px;" id="1"> </div>
<script type="text/javascript">
jwplayer("1").setup({
file: "/Files/view/1/blah.m4v",
});
</script>
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 that the headers are correct and that there are no errant line breaks or data before or after. 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).
My questions, therefor:
- Is fpassthru(), readfile() or similar the correct method for php video passthrough?
- Is this problem specific to my code, or inherent to cakephp's handling of MVC? Has anyone successfully implemented such a solution?
- Are there any other 'thinking outside the box' solutions (using node.js or some other language, .htaccess trickery) that would work better.
- 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?
[Edit] 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:
<?php
$fileName = "/var/www/redacted/files/2/1";
$fp = fopen($fileName, 'rb');
header("Content-Type: video/mp4");
header("Content-Length: 18485");
fpassthru($fp);
?>
The content type, path, and length were taken from the database entry for that file.