Reputation: 71
I would like to download file from remote NAS server, I am not able to force download to client. I am using this function:
public function download(){
// Set IP and port
define("FTP_CONNECT_IP", "xxx");
define("CONNECT_PORT", "21");
// Set username and password
define("FTP_LOGIN_USER", "username");
define("FTP_LOGIN_PASS", "password");
$remote_file = 'ftp://' . FTP_LOGIN_USER . ':' . FTP_LOGIN_PASS . '@' . FTP_CONNECT_IP . '/' .'PathToFile.avi';
$response = $this->response->withFile($remote_file,['download' => true]);
return $response;
}
It starts read something but never browser asks me for download. Please What is wrong?
Upvotes: 0
Views: 1343
Reputation: 60463
You cannot use Response::withFile()
for remote files, it only works with local files.
If you want to serve remote files, then you either have to temporarily store them on your server, or build a proper download response on your own, using for example CakePHPs callback stream for the response body to output the data manually.
Here's a quick example (doesn't support range requests):
return $this->response
->withType(pathinfo($remote_file, \PATHINFO_EXTENSION))
->withDownload(basename($remote_file))
->withLength(filesize($remote_file))
->withBody(new \Cake\Http\CallbackStream(function () use ($remote_file) {
ob_end_flush();
ob_implicit_flush();
readfile($remote_file);
}));
See also
Upvotes: 2