May 04, 2014

10:46 am
In this article, we are going to discuss about How to use the Media view in the cakephp controller. In cakephp Media views allow you to send binary files to the user. For example, you may wish to have a directory of files outside of the webroot to prevent users from direct linking them.

You can use the Media view to pull the file from a special folder within /app/, allowing you to perform authentication before delivering the file to the user.

To use the Media view, you need to tell your controller to use the MediaView class instead of the default View class. After that, just pass in additional parameters to specify where your file is located.

class ExampleController extends AppController {
    function download () {
        $this->view = 'Media';
        $params = array(
              'id' => 'example.zip',
              'name' => 'example',
              'download' => true,
              'extension' => 'zip',  // must be lower case
              'path' => APP . 'files' . DS   // don't forget terminal 'DS'
       );
       $this->set($params);
    }
}

0 comments:

Post a Comment