php ZipArchive 打包下载文件

 

  # 打包下载文件
    public function Download(){
        $id = explode(",", $_GET['id']);
        $type = $_GET['type'];
        $conditionss['mpid'] = get_mpid();
        $conditionss['id']=array('in',$id);
        $data = M('business_emails')->order("visa_time desc")->where($conditionss)->field('username,evisa,receipt')->select();
        foreach ($data as $key => $value) {
            if ($type == 1) {
                $visa[] = $value['evisa'];      # 电子签
                $username[] = $value['username'];      # 姓名
            } else if ($type == 2) {
                $invoice[] = $value['receipt']; # 电子发票
                $username[] = $value['username'];      # 姓名
            } else{
                $visa[] = $value['evisa'];      # 电子签
                $invoice[] = $value['receipt']; # 电子发票
                $username[] = $value['username'];      # 姓名
            }
        }
        if(!empty($visa) || !empty($invoice)){

            $zip = new \ZipArchive();
            //用于前端跳转zip链接拼接
            $path_redirect = '/zip/'.date('Ymd');
            //临时文件存储地址
            $path = '/tmp'.$path_redirect;
            if(!is_dir($path)){
                mkdir($path, 0777,true);
            }
            $filename = time().'download.zip';
            $zipname = $path.'/'.$filename;
            $zip->open($zipname, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
            foreach ($invoice as $key => $url) {
                $fileContent = $this->CurlDownload($url);#curl获取链接内容
                if( $fileContent){
                    $__tmp = $this->SaveFile( $url , $path , $fileContent );# 保存临时文件
                    $names = $username[$key].'.pdf';# 文件名
                    $zip->addFromString( iconv('utf-8', 'gbk//ignore', 'invoice/'.$names), file_get_contents($__tmp[0]));//中文使用这个
                    // if ($__tmp[1]) {
                    //     $names = end(explode("/", $url));# 文件名
                    //     $end  = end(explode("/", $__tmp[0]));
                    // }else{
                    //     $names = end(explode("/", $url)).'.pdf';# 文件名
                    //     $end  = end(explode("/", $__tmp[0])).'.pdf';
                    // }
                    // $zip->addFile($__tmp[0], 'invoice/'.$end);   # 英文数字没问题 中文乱码
                    // $zip->renameName('invoice/'.$end, 'invoice/'.$names);
                    
                }
            }
            foreach ($visa as $key => $url) {
                $fileContent = $this->CurlDownload($url);#curl获取链接内容
                if( $fileContent){
                    $__tmp = $this->SaveFile( $url , $path , $fileContent );# 保存临时文件
                    $names = $username[$key].'.pdf';# 文件名
                    $zip->addFromString( iconv('utf-8', 'gbk//ignore', 'visa/'.$names), file_get_contents($__tmp[0]));//中文使用这个
                    // if ($__tmp[1]) {
                    //     $names = end(explode("/", $url));# 文件名
                    //     $end  = end(explode("/", $__tmp[0]));
                    // }else{
                    //     $names = end(explode("/", $url)).'.pdf';# 文件名
                    //     $end  = end(explode("/", $__tmp[0])).'.pdf';
                    // }
                    // $zip->addFile($__tmp[0], 'visa/'.$end);
                    // $zip->renameName('visa/'.$end, 'visa/'.$names);
                }
            }
            $zip->close();# 关闭连接
            //直接写文件的方式下载到客户端
            if (file_exists($zipname)) {
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header('Content-disposition: attachment; filename=附件.zip'); //文件名
                header("Content-Type: application/zip"); //zip格式的
                header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
                header('Content-Length: ' . filesize($zipname)); //告诉浏览器,文件大小
                @readfile($zipname);
            }
            //删除临时文件
            @unlink($zipname);
        }
    }
    # curl获取链接内容
    public function CurlDownload($url) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $errno   = curl_errno($ch);
        $error   = curl_error($ch);
        $res=curl_exec($ch);
        curl_close($ch);
        if($errno>0){
          return '';
        }
        return $res;
    }
    # 保存临时文件
    public function SaveFile( $url ,$dir , $content){
        $fname   = basename($url); //返回路径中的文件名部分
        $str_name  = pathinfo($fname); //以数组的形式返回文件路径的信息
        $extname  = strtolower($str_name['extension']); //把扩展名转换成小写
        $path    = $dir.'/'.md5($url).$extname;
        $fp     = fopen( $path ,'w+' );
        fwrite( $fp , $content );
        fclose($fp);
        return array( $path , $extname) ;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值