繁体   English   中英

$ 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 /的文件路径一起输出,我不想这样做。 如何使其输出正确的路径? 还是在这种情况下我应该使用其他功能? 任何帮助表示赞赏!

因此,问题实际上是错误的URL。 尽管错误消息很奇怪,但file()函数确实接受完整的文件路径。 感谢您对ndm的评论! 该链接很有帮助。 CakePHP 3-生产站点下载文件路径问题

我来这里是因为一段时间后我遇到了同样的问题,我解决了。 该问题基于:

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()函数返回的APP常量是您的情况下SERVER_ROOT / src /的路径。因此问题出在文件位置。请尝试在本地主机上查找该名称/路径是否存在

您应该在引导文件中对其进行配置。 wwwRoot和wwwWebRoot在这里寻找这两个

https://book.cakephp.org/3.0/en/development/configuration.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM