dongzhuan1185 2013-04-18 20:31
浏览 41
已采纳

Zend Framework 2文件下载

I made a form to upload files to the folder ./data/uploads using the Zend\Filter\File\RenameUpload filter.

This is working like a charm. My problem now is how do I provide this file to users download it?

I think it would be something like:

$response->setContent(file_get_contents('./data/uploads/file.png'));

But I want to know what is the best way to do this.

  • 写回答

2条回答 默认 最新

  • dpz90118 2015-05-21 11:06
    关注

    Thanks to @henrik for response, but several important headers are missing in his answer. Be careful of that.

    Full headers stack:

    public function downloadAction() {
        $file = 'path/to/file';
        $response = new \Zend\Http\Response\Stream();
        $response->setStream(fopen($file, 'r'));
        $response->setStatusCode(200);
        $response->setStreamName(basename($file));
        $headers = new \Zend\Http\Headers();
        $headers->addHeaders(array(
            'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
            'Content-Type' => 'application/octet-stream',
            'Content-Length' => filesize($file),
            'Expires' => '@0', // @0, because zf2 parses date as string to \DateTime() object
            'Cache-Control' => 'must-revalidate',
            'Pragma' => 'public'
        ));
        $response->setHeaders($headers);
        return $response;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码