不清楚 fastadmin 自定义搜索 带入 export 的 可以看上一篇

    /**
     * 导出zip
     */
    public function export(){
        
        $filter = $this->request->param("filter");

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();


        $path =  str_replace('\\','/',realpath(dirname(__FILE__).'/../../../../'))."/public/file/qrcode/".date('Ymd')."/";
        $zip_path =  str_replace('\\','/',realpath(dirname(__FILE__).'/../../../../'))."/public/file/qrcode/zip";
            
            if(!file_exists($path)){
                mkdir($path,0777,true);
            }
            
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->select();

            if(empty($list)){
                return json(array('code'=>1,'error'=>"暂无记录"));
            }
            
            $arr = [];
            foreach ($list as $key => $val) {
                if($val['qrcode_image'] != ""){
                    // 遍历每个,生成对应的图片文件并添加到文件夹
                    $imageData = @file_get_contents(config('site.api_url').$val['qrcode_image']);
                    $imageName = $path . $val['qrcode'] . ".png";
                    file_put_contents($imageName, $imageData);
                }
            }
            
            if(!file_exists($zip_path)){
            mkdir($zip_path,0777,true);
            }
            
            
            $zip_name = date('YmdHis').rand(1000,9999);
            //$url = "/file/qrcode/zip/".$zip_name.".zip";
    
            Makezip::zip($path,$zip_path."/".$zip_name.".zip");
            
            // // 输出到浏览器下载
            header('Content-Type: application/zip');
            header('Content-Transfer-Encoding: Binary');
            header('Content-disposition: attachment; filename='.$zip_name.".zip");
            header('Content-Length: ' . filesize($zip_path."/".$zip_name.".zip"));
            // 这两行很重要,不然可能会出现解压的时候提示文件损坏
            ob_clean();
            flush();
            readfile($zip_path."/".$zip_name.".zip");
            
            // // 删除服务器上的ZIP文件(可选)
            // unlink($zip_path."/".$zip_name.".zip");
            

        

    }

 

posted on 2024-09-09 00:34  cx小橙  阅读(1)  评论(0编辑  收藏  举报