Cakephp 2.3.x Upload files and force download mp4 file

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?

+8
file cakephp download
source share
2 answers

A line you donโ€™t understand is just part of the example - it is assumed that the application has a model called Attachment and that it has a method called getFile . Since you do not have an Attachment model (or at least it is not visible to the controller), you get the error โ€œcall a member function on a non-objectโ€. It's not important though: all you need to worry about is providing a complete system path to this->response->file() . In your example, you can get this by changing this line to:

 $this->response->file(WWW_ROOT.'files/'. $id .'.mp4', array('download' => true, 'name' => 'Dance')); 

You can get rid of the line $this->Attachment->getFile , since that doesn't matter in your case.

Let me know if this helped!

+23
source share
 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'])); ?> 
+1
source share

All Articles