CakePHP 3 - 无论主机和项目名称如何下载

米斯塔克

在任何特定视图上,如果我包含此链接:

<?php echo $this->Html->link('Download', ['controller' => 'Uploads', 'action' => 'download',$upload->id]) ?>

用户可以根据$upload->id提供的内容下载文件

我在我的上传控制器中使用它作为我的下载功能:

$upload = $this->Uploads->find('all')->first();
$filePath = '\xampp\htdocs\project\webroot\uploads'; //file path to where the uploaded file is
$this->response->file($filePath . DS . $upload->name,
    array('download' => true, 'name' => $upload->name)); //finds the corresponding name attribute in the Uploads table of the database, finds a matching name in the uploads directory and then downloads that file.

$this->set(compact('upload'));

但是,我想知道是否有办法让文件路径更灵活。在我的 中config.php,我设置了两个变量,称为$host$basepath分别引用网络主机(本地主机)和项目名称(项目)。我将这些变量传递给 AppController,它们可以被任何视图访问。

但是我似乎无法$filePath在下载函数变量中使用这些变量,因此如果我或其他任何人要更改主机或项目名称,而不是能够简单地更改config.php,我或其他任何人将不得不去找到并更改它(如果我在多个控制器中有多个下载功能,他们将不得不去更改每一个)。

马里扬

查看 CakePHP 全局常量。您可以使用 轻松访问 webroot 的路径WWW_ROOT

https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章