I'm using cakephp 2.3.1
I want to force download a mp4 file per http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file
In my 'view' I have the following code which is correctly searching for the filename, and finding the file name, and displaying 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 the download of the mp4 file. In my controller I have the following code that doesn't 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 don't understand 'Attachment' and the 'getFile()'
I'm getting the following error: Error: Call to a member function getFile() on a non-object
What am I doing wrong and is there any other documentation I can be looking at to understand this better?
The line you don't understand is simply part of the example - it assumes the application has a model called Attachment
and that it has a method called getFile
. Since you don't have an Attachment
model (or at least it isn't visible to the controller) you get a "call to member function on a non-object" error. That's not important though: all you need to worry about is providing a full system path to this->response->file()
. In your example, you can probably get that by changing that line to:
$this->response->file(WWW_ROOT.'files/'. $id .'.mp4', array('download' => true, 'name' => 'Dance'));
You can get rid of the $this->Attachment->getFile
line as it is irrelevant in your case.
Let me know if that helped!
public function downloadfile($id= null) {
$this->response->file(APP.'webroot\files\syllabus'.DS.$id,array('download'=> true, 'name'=>'Syllubus'));
return $this->response;
}
<?php echo $this->Html->link('Syllabus',
array('controller' => 'coursesubjects',
'action'=>'downloadfile',
$courseSubject['CourseSubject']['subject_syllabus']));
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With