PHP对框架系统文件的更新升级功能

这段代码实现了一个自动检查和下载新版本的功能,通过HTTP请求获取远程版本信息,对比本地版本,如果不同则下载更新的ZIP文件,解压并移动文件到指定位置,同时处理目录和文件的更新及SQL导入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

逻辑:两个网址。一个网址发布新的版本信息,存放升级压缩包。另外一个获取并且比较当前版本并更新。被更新网站我是自己建了个version文件夹,远程下载的压缩包下载到文件夹中,解压。移动文件夹到指定位置。

    /**
     * @description:  版本检测并下载
     */
    public function version_check()
    {
        $temp = scandir('./version/');
        foreach($temp as $value)
        {
            if($value=='.' || $value =='..'){
                continue;
            }
          $version = $value;  //本地最新版本号 
        }
        $info = Http::post('https://*****/api/version/index');  //获取更新信息
        $info = json_decode($info);
        $file = $info->version.'.zip';      //压缩包名是版本号
        $new_version = $info->version; //最新版本号
        $down_url =$info->downloadurl;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $down_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //规避SSL验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //跳过HOST验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $st = curl_exec($ch);   //将curl的结果存到变量里
        if($version!==$new_version){
            if(file_exists("./version/$file")){
                echo "目录已存在";exit;
            } 
            mkdir('./version/'.$new_version); //创建新的版本目录
            if(file_exists("./version/$new_version/$file")){
                echo "文件已存在";exit;
            }

            $fd = fopen("./version/$new_version/$file", 'w'); //文件写入版本目录里
            fwrite($fd, $st);  //将curl的结果写入文件里
            fclose($fd);
            $new_path ="./version/$new_version/$file";//要解压的文件路径
            $this->update_file($new_path,$new_version);
            return  "恭喜你已经更新到新的版本$new_version";
        }else{
            return "目前已是最新版本:$version";
        } 
        curl_close($ch);
    }
    
   
   
    /**
     *  @description: 更新文件
     * @param string $file 文件
     * @param string $new_version 新的版本地址
     * @return  boolean
     */
    public function update_file($files,$new_version)
    {
        $zip = new \ZipArchive();
        $openRes = $zip->open($files);
        $outPath = "./version/$new_version/";     //输出路径
        if ($openRes === TRUE) {
            $zip->extractTo($outPath);   //解压文件
            $zip->close(); //关闭zip
        }
        unlink($files);    //删除ZIP文件
        if ($dh = opendir($outPath)){ //打开目录解压后的目录  version/V2.0/下的
            while (($file = readdir($dh)) !== false){
                if($file=='.' || $file=='..'){
                    continue;
                }
                $newPaths = $outPath.$file;  
                $new_dir_path="./../";  //放在本地的目录地址
                if(is_dir($newPaths)){  // version/2.0/application 是否为目录 
                    $this->dirscran($outPath,$file,$new_dir_path);
                    rmdir($newPaths);
                }else{
                    if($file=='update.sql'){
                        $sqlFile = $outPath.'/'.$file;
                        $this->importsql($sqlFile);
                        unlink($sqlFile);
                    }else{
                        $old_file = $outPath.$file; //旧文件
                        $newFile = $new_dir_path.$file; //新文件
                        copy($old_file,$newFile); //拷贝到新文件
                        rename($old_file,$newFile);  
                    }
                }
            }
        }  
    }
    
    /**
     * @description: 查子文件和移动文件
     * @param string $new_path 目录地址
     * @param string $testfile文件
     * @param string $new_dir_path //放在新的本地目录地址
     * @return  boolean
     */
   public function dirscran($new_path,$testfile,$new_dir_path)
   {    
       $scan_path = $new_path.'/'.$testfile; //例如application目录
       $dir_path = $new_dir_path.$testfile.'/'; //本地目录的application目录
       // Open a directory, and read its contents
       if (is_dir($scan_path)){           
           if ($dh = opendir($scan_path)){
               while (($file = readdir($dh)) !== false){
                   if($file=='.' || $file=='..'){
                       continue;
                   }
                   $filedir = $scan_path.'/'.$file; //新的目录地址
                   if(is_dir($filedir)){
                       $this->dirscran($scan_path,$file,$dir_path);
                       rmdir($filedir); //删除文件夹
                   }else{  
                       $old_file="$scan_path/$file"; //旧文件
                       $newFile=$dir_path.$file; //新文件         
                       copy($old_file,$newFile); //拷贝到新文件
                       rename($old_file,$newFile);
                    //    unlink($file);
                   }
               }
                closedir($dh);
           }
       }
    
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值