Skip to content

CakePHP force download

by Vinícius Krolow on maio 12th, 2010

A common stuff that we need do using PHP is force the download of files, a common routine for it is when we can not give access for the users for the folder where are the files, so we must force the download.

To do it with CakePHP is very simple, for it we have the Media view, let’s go to the code.

some_controller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< ? php
class SomeController extends AppController {
 
    public $uses = null;
 
    public function download() {
         $this->view = 'Media';
         $params = array(
                'id' => 'video.flv',
                'name' => 'video',
                'download' => true,
                'extension' => 'flv',
                'path' => APP . 'files' . DS
        );
        $this->set($params);
    }
 
}
?>

So let’s explain quickly, we set the id of the file, the name of the file, the extension, and the path indicate where is the file, and the important set the download with true and so pass it for the view Media, and the magic happens, the class Media get the hard work and force the download.

You can see more things that Media can do for you in the CakePHP Book.

From → cakephp, php

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS