Zend framework2-下载文件方法
2015-09-13 14:16
666 查看
一、写法一
二、写法二
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; }
二、写法二
public function downloadAction() { $fileName = 'somefile'; $response = new \Zend\Http\Response\Stream(); $response->setStream(fopen($fileName, 'r')); $response->setStatusCode(200); $headers = new \Zend\Http\Headers(); $headers->addHeaderLine('Content-Type', 'whatever your content type is') ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"') ->addHeaderLine('Content-Length', filesize($fileName)); $response->setHeaders($headers); return $response; }
相关文章推荐
- FTP传输Port模式和 Passive模式
- PHP 性能分析与实验:性能的微观分析
- Zend Framework2-验证上传文件大小和扩展名,直接在调用函数验证的方法
- OLTP和OLAP的区别
- PhpStorm 8.x/9.x 快捷键设置/个性化设置,如何多项目共存?如何更换主题?
- 穿越两次PIX8.0防火墙并两次静态NAT的FTP测试
- 穿越两次PIX8.0防火墙并两次静态NAT的FTP测试
- 教你识别简单的免查杀PHP后门
- 配置FTP服务(二):vsftpd部署和优化
- php的时间输出格式
- php mysqli 扩展配置
- 多重方法 验证php是否开启某项扩展
- CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
- PHP入门-摘要表格处理问题
- PHP的foreach和其它循环的区别
- wamp无法进入phpMyAdmin或localhost的解决方法
- 那些年遇到的php之坑
- php的public、protected、private三种访问控制模式的区别
- PHP中两个整形的赋值所占用空间大小不一样,求解
- php冒泡排序程序