thinkphp6: 在extend目录下开发扩展类(php 8.1.1 / thinkphp v6.0.10LTS )

一,extend目录的功能:

      extend目录可以存放自己所开发的具备通用功能的类,
       vendor目录存放的是通过composer下载的第三方库
       注意与app目录下的内容区分

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/29/thinkphp6-zai-extend-mu-lu-xia-kai-fa-kuo-zhan-lei-php-8-1/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,编写php代码:

1,controller/Article.php
<?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'=>'创建失败'];
        }
    }
}

三,测试效果

1,访问:
http://192.168.219.6:8000/article/zip
返回:
2,查看生成的zip包:

四,查看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 

 

posted @ 2022-01-27 21:42  刘宏缔的架构森林  阅读(1290)  评论(0编辑  收藏  举报