zen of coding

A few words about media views (sending binary files to the user)

Media views is one really nice feature of CakePHP which doesn’t get much attention, so I figured to briefly cover it here. Actually the manual lists pretty much all you need to know about media views, but there are a couple of things that I’d like to point out. First, take a look at the brief example and more detailed explanation here: http://book.cakephp.org/view/489/Media-Views

One might ask what’s the point of using ‘id’ and ‘name’? Well, the ‘name’ key allows you to change the file name as it will be displayed to the user (in a “save” dialog, for example), it is not the actual name of the file on the server or at least it doesn’t have to be. For example, if you have a reporting tool that generates some files and their name on the server happens to be 329569364658875.zip, you can modify such ugly name on the fly by specifying: ‘name’ => ‘user_friendly_file_name’.

Also, remember that ‘path’ is relative to your app directory and requires a trailing DS (directory separator). Therefore if you have a path like /home/bob/cake/app/files_for_download, you specify:

And just to reiterate what the manual says, media views allow you to perform authentication before delivering content and you can deliver files which are not accessible from webroot to prevent direct linking.

P.S. Thanks to skiedr and Tarique Sani who’ve pointed out that you can now cache the files generated by media views, which ultimately means that images, etc. can be fetched from the browser cache if available.

  • That’s pretty neat, I didn’t know about this yet. It’s definitely useful if you need to protect files which should only be available to certain users of your site, this might come in handy sometime, thanks! :)

  • teknoid


    You’re welcome

  • skiedr

    Recently was added ability to cache files generated by media view. It is highly increase perfomance of Media view.

  • teknoid


    Thanks for pointing this out.

  • To further what skieder added – the cache is in fact the ability to send cache headers so that images etc can be fetched from Browser cache if present

  • teknoid

    @Tarique Sani

    Thanks for your comment, I’ve updated the post to reflect yours and skiedr’s suggestions.

  • matt c

    can someone explain how you would apply caching to the media view?

  • prabhaswara

    why i can’t download .rar files
    zip,exe,png, it’s ok..

  • @prabhaswara

    Not sure, could be something with your server settings.

  • As an aside, I’ve only just noticed that the Auth component works completely through PHP (duh), but blindly assumed my entire subdomain which was running through Auth (and had no guest access) would block access to ALL files in that subdomain. This is not the case if people are emailing direct linked image files, PDFs, or documents that are contained within the document root (I was incorrectly thinking of htaccess redirection apparently).

    Media Views, although requiring a reworking of how you display data, will allow you to serve these documents from outside the document root (except for layout design images in webroot) only after a user is authenticated. The reason for this is that a PDF document, for instance, does not invoke the PHP interpreter, therefore none of the magic of Auth can be run before rendering the document. Although there are some htaccess redirect tactics one could take to attempt to alleviate this, the Media View is probably the most reasonable and effective solution (as it also gives you more control and options).

  • @Brendon Kozlowski

    Great observations, thanks for sharing. If you don’t mind, I’d like to add them to the post.

  • Can’t seem to get caching to work headers are still 200 ok instead of 304.
    Anybody got a working example?

%d bloggers like this: