【问题标题】:Download file CakePHP 3下载文件 CakePHP 3
【发布时间】:2015-10-29 20:55:10
【问题描述】:

我尝试下载上传的文件,这些文件保存在 webroot 的“文件”文件夹中。这是我在控制器中的下载功能:

   public function download($id=null) { $this->response->file(WWW_ROOT . DS .'files'. $id ,array('download'=> true,'id'=> 'file name.pdf',
        'name'=> 'file name'));

在视图中我有以下内容:

   <td>  <?= $this->Html->link('Download', ['controller' => 'Articles', 'action' => 'download',$id]) ?></td>

首先这会导致下载按钮下的错误(未定义的变量 id),然后我得到(更全局的)错误:请求的文件 C:\xampp\htdocs\articles\src\C:\xampp\ htdocs\articles\webroot\files 未找到或不可读。我真的不明白为什么 C:\xampp\htdocs\articles 部分重复两次以及如何解决这些问题并下载上传的文件(不仅是函数 $this->response->file 中的指定文件,还有通过它的 ID)。任何帮助将不胜感激!

【问题讨论】:

  • 您是否尝试删除文件路径中的WWW_ROOT . DS .

标签: php cakephp cakephp-3.0


【解决方案1】:
public function file_download()
{
    $file_path = WWW_ROOT.'uploads'.DS.'file_name.doc';
    $this->response->file($file_path, array(
        'download' => true,
        'name' => 'file_name.ppt',
    ));
    return $this->response;
}

【讨论】:

  • oic,所以return $this-&gt;response 很重要,明白了!
【解决方案2】:

我不认为file()id 选项,WWW_ROOT 后面也不需要DS

public function download($id=null) { 
    $filePath = WWW_ROOT .'files'. DS . $id;
    $this->response->file($filePath ,
        array('download'=> true, 'name'=> 'file name'));
}

路径重复,因为您的文件路径错误,file()APP 添加到您提供的路径之前。

如果路径不是解析为文件的绝对路径,APP 将被添加到路径之前。

source

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 2016-08-07
相关资源
最近更新 更多