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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现