$this->response->file() CakePHP3 文件路径设置
File path setting of $this->response->file() CakePHP3
我正在尝试使用 CakePHP3 创建文件下载功能,但无法将正确的文件路径指向 $this->response->file();这是我的函数:
public function download($id)
{
$attachment = $this->Attachments->get($id);
$this->response->file(WWW_ROOT.$attachment->filepath.DS.$attachment->filename);
$return $this->response;
}
file() 函数中的完整文件路径是正确的。但是,file() 函数输出带有额外的路径。
正确的文件路径是SERVER_ROOT/public_html/attachments/filename
输出路径为SERVER_ROOT/src//SERVER_ROOT/public_html/attachments/filename
因此,file() 函数似乎输出了 src/ 的文件路径,我不想这样。我怎样才能让它输出正确的路径?或者我应该在这种情况下使用另一个功能吗?感谢任何帮助!
您应该在 bootstrap 文件中配置它。 wwwRoot 和 wwwWebRoot 在这里寻找这两个
https://book.cakephp.org/3.0/en/development/configuration.html
所以,问题其实是错误的URL。尽管错误消息很奇怪,但 file() 函数确实接受完整的文件路径。感谢 ndm 的评论! link 很有帮助。
CakePHP 3 - Production site download file path issue
我来这里是因为我遇到了同样的问题,过了一会儿我解决了。
问题基于:
protected function validateFile($path)
{
if (strpos($path, '../') !== false || strpos($path, '..\') !== false) {
throw new NotFoundException(__d('cake', 'The requested file contains `..` and will not be read.'));
}
if (!is_file($path)) {
$path = APP . $path;
}
$file = new File($path);
if (!$file->exists() || !$file->readable()) {
if (Configure::read('debug')) {
throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
}
throw new NotFoundException(__d('cake', 'The requested file was not found'));
}
return $file;
}
validateFile() 函数 returns 带有 APP 常量,它是您 case.So 中 SERVER_ROOT/src/ 的路径问题是要查找的文件 location.Try在你的本地主机上,如果它甚至存在 name/path
我正在尝试使用 CakePHP3 创建文件下载功能,但无法将正确的文件路径指向 $this->response->file();这是我的函数:
public function download($id)
{
$attachment = $this->Attachments->get($id);
$this->response->file(WWW_ROOT.$attachment->filepath.DS.$attachment->filename);
$return $this->response;
}
file() 函数中的完整文件路径是正确的。但是,file() 函数输出带有额外的路径。
正确的文件路径是SERVER_ROOT/public_html/attachments/filename
输出路径为SERVER_ROOT/src//SERVER_ROOT/public_html/attachments/filename
因此,file() 函数似乎输出了 src/ 的文件路径,我不想这样。我怎样才能让它输出正确的路径?或者我应该在这种情况下使用另一个功能吗?感谢任何帮助!
您应该在 bootstrap 文件中配置它。 wwwRoot 和 wwwWebRoot 在这里寻找这两个
https://book.cakephp.org/3.0/en/development/configuration.html
所以,问题其实是错误的URL。尽管错误消息很奇怪,但 file() 函数确实接受完整的文件路径。感谢 ndm 的评论! link 很有帮助。 CakePHP 3 - Production site download file path issue
我来这里是因为我遇到了同样的问题,过了一会儿我解决了。 问题基于:
protected function validateFile($path)
{
if (strpos($path, '../') !== false || strpos($path, '..\') !== false) {
throw new NotFoundException(__d('cake', 'The requested file contains `..` and will not be read.'));
}
if (!is_file($path)) {
$path = APP . $path;
}
$file = new File($path);
if (!$file->exists() || !$file->readable()) {
if (Configure::read('debug')) {
throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
}
throw new NotFoundException(__d('cake', 'The requested file was not found'));
}
return $file;
}
validateFile() 函数 returns 带有 APP 常量,它是您 case.So 中 SERVER_ROOT/src/ 的路径问题是要查找的文件 location.Try在你的本地主机上,如果它甚至存在 name/path