使用CakePHP中的媒体视图下载文件

使用CakePHP中的媒体视图下载文件

本文介绍了使用CakePHP中的媒体视图下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过4个不同的链接下载4个不同的文件。我使用媒体视图下载文件,但我必须在控制器的下载功能硬编码文件名:

I want to download 4 different files through 4 different links. I am using the Media view to download the files, but I have to hardcode the file name in the download functions in the controller:

function download () {
    $this->view = 'Media';
    $params = array(
          'id' => 'example.zip',
          'name' => 'example',
          'download' => true,
          'extension' => 'zip',
          'path' => APP . 'files' . DS
   );
   $this->set($params);
}

这适用于一个文件。现在,对于链接数2,3,4,我需要创建3种不同的动作,并给它们不同的文件名,或有一种方式,我可以使用download()只下载相应的文件,取决于哪个链接已被点击?

This works fine for one file. Now, for links number 2,3,4, do I need to create 3 different actions and give different file names in them, or is there a way in which I can use download() to only download the respective file depending on which link has been clicked?

推荐答案

这是变量的用途。通用示例:

That's what variables are for. Generic example:

function download($fileId) {
    $file = // find the file you want to serve based on $fileId
    $pathInfo = pathinfo($file['path']);

    $this->view = 'Media';
    $params = array(
          'id'        => $file['name'],
          'name'      => $pathInfo['filename'],
          'extension' => $pathInfo['extension'],
          'download'  => true,
          'path'      => APP . 'files' . DS
   );
   $this->set($params);
}

这篇关于使用CakePHP中的媒体视图下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:03