说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
<?php declare (strict_types = 1); namespace app\controller; use app\BaseController; use zip\zip; class Article extends BaseController { //生成zip文件 public function zip() { $pathArr = [ "/var/www/html/goodsimage/orig/61e7c3a0327ad.gif", "/var/www/html/goodsimage/orig/gsl.jpeg", ]; $zipName = "/var/www/html/goodsimage/image.zip"; $zip = new zip(); $res =$zip->makeZip($pathArr,$zipName); //if ($res['code']) echo $res['code']; exit; } }2,extend/zip/zip.php
<?php namespace zip; 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'=>'创建失败']; } } }
http://192.168.219.6:8000/article/zip返回: 2,查看生成的zip包:
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 Technologiesthinkphp:
liuhongdi@lhdpc:/var/www/html$ cd /data/php/admapi/ liuhongdi@lhdpc:/data/php/admapi$ php think version v6.0.10LTS