Shuhei.F Shuhei.F - 1 month ago 6
PHP Question

File path setting of $this->response->file() CakePHP3

I'm trying to make a file download function with CakePHP3 and can't make it to path the correct file path to $this->response->file(); Here's my function:

public function download($id)
{
$attachment = $this->Attachments->get($id);
$this->response->file(WWW_ROOT.$attachment->filepath.DS.$attachment->filename);
$return $this->response;
}


The full file path inside file() function is correct. However, file() function outputs with extra path.

The correct file path is SERVER_ROOT/public_html/attachments/filename

The output path is SERVER_ROOT/src//SERVER_ROOT/public_html/attachments/filename

So, file() function seems to output with the file path for src/, which I don't want to. How can I make it to output the correct path? Or am I supposed to use another function in this case? Any helps are appreciated!

Answer Source

So, the problem was actually the wrong URL. Though the error message was weird, file() function does accept a full file path. Thanks for the comments ndm! That link was helpful. CakePHP 3 - Production site download file path issue