博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php的文件下载
阅读量:4636 次
发布时间:2019-06-09

本文共 1842 字,大约阅读时间需要 6 分钟。

 * 下载文件

     *
     * @param string $downloadFile            
     * @param string $downloadName            
     */
    private function downLoad($downloadFile, $downloadName) {
        header ( "Cache-Control: public" );
        header ( "Content-Description: File Transfer" );
        header ( 'Content-disposition: attachment; filename=' . $downloadName ); // 文件名
        header ( "Content-Type: application/zip" ); // zip格式的
        header ( "Content-Transfer-Encoding: binary" ); // 告诉浏览器,这是二进制文件
        header ( 'Content-Length: ' . filesize ( $downloadFile ) ); // 告诉浏览器,文件大小
        @readfile ( $downloadFile );
    }
    ------------------------------------------------------------------------------------

/**

     * 客户授信申请附件下载
     */
    public function attachmentDownloadAction(){
        $request = $this->getRequest();
        
        $filePath = $request->getParam('filePath');
        $filePath = Zend_Filter::filterStatic ( $filePath, 'StringTrim' );
        $filePath = Zend_Filter::filterStatic ( $filePath, 'StripTags' );
        $filePath = APPLICATION_PATH."/../public".$filePath;
        
        $fileName = $request->getParam('fileName');
        $fileName = Zend_Filter::filterStatic ( $fileName, 'StringTrim' );
        $fileName = Zend_Filter::filterStatic ( $fileName, 'StripTags' );
        $fileName = iconv("UTF-8", "GB2312", $fileName);
        
        if (file_exists($filePath)) {
            $this->attachmentDownload($filePath,$fileName);
        } else {
            throw new Member_Model_NotExist_Exception('附件不存在!');
        }
    }
    private function attachmentDownload ($filePath,$fileName)
    {
        $file = file_get_contents($filePath);
        $this->getResponse()
            ->setBody($file)
            ->setHeader('Content-Type', 'application/octet-stream')
            ->setHeader('Content-Disposition',
                'attachment; filename="'.$fileName.'"')
            ->setHeader('Content-Length', strlen($file));
        
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        header('Set-Cookie: fileDownload=true; path=/');
        $this->getResponse()->sendResponse();
    }

转载于:https://www.cnblogs.com/eterwei/p/3927825.html

你可能感兴趣的文章
Java学习笔记
查看>>
sprintf 和strcpy 的差别
查看>>
jQuery_第五章_jQuery事件和动画
查看>>
打表打表何谓打表?
查看>>
MPEG4与.mp4
查看>>
实验5
查看>>
成长轨迹44 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2799、2976、2975、2742】...
查看>>
git 下载 安装
查看>>
录制终端信息并回放
查看>>
JS中window.event事件使用详解
查看>>
ES6深入学习记录(一)class方法相关
查看>>
Linux 文件系统及 ext2 文件系统
查看>>
jenkins ssl证书报错问题解决
查看>>
《BI项目笔记》用Excel2013连接和浏览OLAP多维数据集
查看>>
C语言对mysql数据库的操作
查看>>
SQL Server 数据库备份
查看>>
INNO SETUP 获得命令行参数
查看>>
http编程学习(C#)
查看>>
DNN 数据访问策略 (转)
查看>>
Sublime Text 自动换行
查看>>