I am using cakephp 2.3.1
I want to force download an mp4 file for http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file
In my "view" I have the following code that correctly looks for the file name and finds the file name and shows the download link:
<?php $filename = APP . 'webroot/files/' . $dance['Dance']['id'] . '.mp4'; if (file_exists($filename)) { echo $this->Html->link('DOWNLOAD', array('controller' => 'dances', 'action' => 'sendFile', $dance['Dance']['id'])); } else { echo 'Coming soon: available April 16th'; } ?>
When the user clicks on the link, I want to force download the mp4 file. In my controller, I have the following code that does not work:
public function sendFile($id) { $file = $this->Attachment->getFile($id); //Note: I do not understand the 'Attachment' and the 'getFile($id)' $this->response->file($file['webroot/files/'], array('download' => true, 'name' => 'Dance')); //Return reponse object to prevent controller from trying to render a view return $this->response; }
I do not understand "Attachment" and "getFile ()"
I get the following error: Error: calling the getFile () member function for a non-object
What am I doing wrong, and is there any other documentation that I can look at in order to understand this better?
file cakephp download
user2133231
source share