我想从远程NAS服务器下载文件,我无法强制下载到客户端。我正在使用这个功能:
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;
}
它开始阅读一些东西,但从来没有浏览器要求我下载。请问有什么问题?
答案 0 :(得分:2)
您不能将Response::withFile()
用于远程文件,它只适用于本地文件。
如果要提供远程文件,则必须将它们临时存储在服务器上,或者自己构建正确的下载响应,例如使用CakePHP回调流为响应主体手动输出数据。 / p>
这是一个快速示例(不支持范围请求):
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);
}));
另见