2

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">&nbsp;</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:

  1. Is fpassthru(), readfile() or similar the correct method for php video passthrough?
  2. Is this problem specific to my code, or inherent to cakephp's handling of MVC? Has anyone successfully implemented such a solution?
  3. Are there any other 'thinking outside the box' solutions (using node.js or some other language, .htaccess trickery) that would work better.
  4. 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.

Community
  • 1
  • 1
Kim Blyth
  • 65
  • 7
  • Did you get this figured out? – Hoff Feb 25 '13 at 21:12
  • No, sorry - I modified the application to access files directly via the cakephp webroot directory. The web site was very localised so it was unlikely anyone else would spend the effort to steal the few videos that were on there. I will definitely try your implementation though when I get back around to developing that site. – Kim Blyth Apr 02 '13 at 03:56

1 Answers1

1

Cake has the functionality you want built in, check out Media Views in the documentation. Note that it's deprecated in Cake 2.3.0, but since you're using 2.0, it should work just fine. I tested the following code and had no problem with it playing a sample m4v in jwplayer or the VLC plugin:

$this->viewClass = 'Media';

$fileName = WWW_ROOT . DS . 'sample_iPod.m4v';

$parts = pathinfo($fileName);

$params = array(
    'id' => $parts['basename'],
    'name' => $parts['filename'], // requires PHP >= 5.2.0
    'extension' => $parts['extension'],
    'download' => false,
    'path' => $parts['dirname'] . DS
);

$this->set($params);

Note that Cake will automatically output the correct content type/length for you based on the extension.

Hoff
  • 1,762
  • 14
  • 27