I am writing a simple application with cakePHP 2.x. I need to allow user to uplaod and download files. The Upload works fine, but the i’m stuck with the download action.
I have a Controller name Documents, containing the Download action:
public function download($id = null) {
$this->Document->recursive = -1;
$doc = $this->Document->find('first', array('conditions' => array('Document.id' => $id)));
$filename = $doc['Document']['file_file_name'];
$resp = new CakeResponse();
$resp->download(Configure::read('upload_dir').'upload'.DS.$id.'_'.$filename);
$resp->send();
}
Yes I didn’t check if the file exists, etc… but it’s just for testing.
So the path in the download method is something like: /home/www-app/upload/$id_$filename
Of course, the file exists and both paths are equal.
But I got the following error from chrome:
Erreur 6 (net::ERR_FILE_NOT_FOUND) : File or Directory not found
I tried the $resp->file() method but cakePHP seems to not know that method.
Thanks!
you are not working with Cake2.x and its response class the way you should (and how it is documented!)
dont use a new instance, there already is one you need to use:
etc.
also, using autoRender false you dont need a view (what for if you directly send the file?).
Correction 2013-01-10:
You dont even need the send(). the autoRender part itself should suffice.
The response class will then automatically invoke send() at the end of the dispatching process: