💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 一,编写php代码 1,lib/util/zip.php ~~~ <?php namespace app\lib\util; class zip { //打包一个zip文件 public function makeZip($pathArr,$zipName) { $zip = new \ZipArchive(); if($zip->open($zipName,\ZipArchive::CREATE|\ZipArchive::OVERWRITE)){ foreach($pathArr as $file){ if(!file_exists($file)){ continue; } //向压缩包中添加文件 $zip->addFile($file,basename($file)); } $zip->close(); return ['code'=>0,'msg'=>"创建成功",'path'=>$zipName]; }else{ return ['code'=>1,'msg'=>'创建失败']; } } } ~~~ 2,controller中调用 ~~~ <?php declare (strict_types = 1); namespace app\controller; controller/Article.php use app\lib\util\zip; class Article extends BaseController { //生成zip文件 public function zip() { $pathArr = [ "/var/www/html/goodsimage/orig/61e386409e626.jpg", "/var/www/html/goodsimage/orig/61e38640dca86.jpg", ]; $zipName = "/var/www/html/goodsimage/image.zip"; $zip = new zip(); $res =$zip->makeZip($pathArr,$zipName); //if ($res['code']) echo $res['code']; exit; } //下载zip文件 public function downZip() { $zipName = "/var/www/html/goodsimage/image.zip"; return download($zipName,"image.zip"); } } ~~~          对应的源码可以访问这里获取: [https://github.com/liuhongdi/ ](https://github.com/liuhongdi/)         或: [https://gitee.com/liuhongdi](https://gitee.com/liuhongdi) ## 二,测试效果 1,生成zip: 查看图片: ![](https://img.kancloud.cn/38/48/38483f8afae5cf64c1ea9a2e67c33e76_699x160.png) 访问url: ``` http://192.168.219.6:8000/article/zip ``` 返回: ![](https://img.kancloud.cn/ed/04/ed04be0cab09e75e8818898464cde5d3_828x136.png) 查看生成的压缩包: ![](https://img.kancloud.cn/ea/27/ea27757051305ffb82aaa8fdf8c13a4e_797x300.png) 注意:保存生成的zip包文件的路径需要有写入权限 2,下载zip包: 访问: ``` http://192.168.219.6:8000/article/downzip ``` 返回: ![](https://img.kancloud.cn/9e/cb/9ecb05cbb5c329cfbf6113edb228a4af_1544x656.png) ## 三,查看php和thinkphp的版本:  php: liuhongdi@lhdpc:/data/php/admapi$ php --version PHP 8.1.1 (cli) (built: Dec 20 2021 16:12:16) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.1, Copyright (c) Zend Technologies with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies  ~~~ thinkphp: ~~~ liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS ~~~