ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### **全新示例** ~~~ public function index() { //更新包检测地址 $version = $this->version; try { $request = \think\Request::instance(); $result = Http::get('你的域名'.'/api/index/index', ["version"=>$version,'domain'=>$request->root(true)]); } catch (\Exception $e) { return json(["msg" => "更新包获取失败,请重试!", "code" => 400]); } $result = json_decode($result, true); if(empty($result)){ return json(["msg" => "更新包获取失败,请重试!", "code" => 400]); } if($result["code"] == 1 && $this->gengxin = true){ //循环更新完毕 //更新完成后刷新配置文件 $this->refreshFile(); //清除站点缓存 rmdirs(CACHE_PATH, false); Cache::clear(); return json(["msg" => "更新完成!请刷新页面", "code" => 200]); } //code为400的时候代表没有更新包 if ($result["code"] == 400) { //没有需要更新的版本 return json(["msg" => $result["msg"], "code" => 400]); } $this->gengxin = true; //开始更新版本 //更新包信息 $upgrade = $result["data"]; $file_url = $upgrade["file"]; $filename = basename($file_url); $dir = ROOT_PATH . "runtime/upgrade/"; if (!file_exists($dir)) { mkdir($dir, 0777, true); } $path = file_exists($dir . $filename) ? $dir . $filename : $this->download_file($file_url, $dir, $filename); $zip = new \ZipArchive(); //打开压缩包 if ($zip->open($path) === true) { $toPath = ROOT_PATH; try { //解压文件到toPath路径下,用于覆盖差异文件 $zip->extractTo($toPath); unlink($path); //删除更新包 } catch (\Exception $e) { return json(["msg" => "没有该目录[" . $toPath . "]的写入权限", "code" => 400]); } //文件差异覆盖完成,开始更新数据库 if(file_exists(ROOT_PATH . "/sql.php")){ include ROOT_PATH . "/sql.php"; chmod(ROOT_PATH . "/sql.php",0777); unlink(ROOT_PATH . "/sql.php"); } //更新后台静态文件版本 db::name('config')->where(['name' => 'version'])->update(['value' => time()]); $this->version = $upgrade["version"]; //更新本次版本号准备检测下个版本 return $this->index(); //递归更新 } else { unlink($path); //删除更新包 return json(["msg" => "更新包解压失败,请重试!", "code" => 400]); } } /** * 刷新配置文件 */ protected function refreshFile(){ $config = []; $list = db::name('config')->select(); foreach ($list as $k => $v) { $value = $v; if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) { $value['value'] = explode(',', $value['value']); } if ($value['type'] == 'array') { $value['value'] = (array)json_decode($value['value'], true); } $config[$value['name']] = $value['value']; } file_put_contents( CONF_PATH . 'extra' . DS . 'site.php', '<?php' . "\n\nreturn " . var_export_short($config) . ";\n" ); } public function download_file($url, $dir, $filename = '') { if (empty($url)) { return false; } $ext = strrchr($url, '.'); $dir = realpath($dir); //目录+文件 $filename = (empty($filename) ? '/' . time() . '' . $ext : '/' . $filename); $filename = $dir . $filename; //开始捕捉 ob_start(); readfile($url); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); $fp2 = fopen($filename, "a"); fwrite($fp2, $img); fclose($fp2); return $filename; } ~~~ ### **原生示例** ~~~ /*添加到需要授权php源码顶部----复制添加时请把 ?换成 ? */ 请添加在<?php ?> 里面 class UpdateAction extends BackAction{ public function index(){ $version = './Data/version.php'; $ver = include($version); $ver = $ver['ver']; $sysnum = include($version); $sysnum = $sysnum['sysnum']; $ver = substr($ver,-3); $updatehost = 'http://<? echo $_SERVER['SERVER_NAME']; ?>/oreo_look.php'; $lastver = file_get_contents(($updatehost . '?a=check&v=') . $ver .'&sysnum='. $sysnum); if($lastver !== $ver){ $updateinfo = ('<p class="red">最新版本为:源分享系统v ' . $lastver) . '</p><span> <a href="javascript:if(confirm(\'升级前,请确认已经做好数据库和程序备份!\'))location=\'./index.php?g=System&m=Update&a=updatenow\'">点击这里在线升级</a> </span>'; $chanageinfo = file_get_contents(($updatehost . '?a=chanage&v=') . $lastver .'&sysnum='. $sysnum); }else{ $updateinfo = ('<p class="red">最新版本为:源分享系统v ' . $lastver) . '</p><span>已经是最新系统 不需要升级</span>'; } $this -> assign('updateinfo', $updateinfo); $this -> assign('chanageinfo', $chanageinfo); $this -> display(); } public function updatenow(){ include('Update.class.php'); $version = './Data/version.php'; $ver = include($version); $ver = $ver['ver']; $sysnum = include($version); $sysnum = $sysnum['sysnum']; $ver = substr($ver,-3); $hosturl = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); $updatehost = 'http://<? echo $_SERVER['SERVER_NAME']; ?>/oreo_look.php'; $updatehosturl = $updatehost . '?a=update&v=' . $ver . '&u=' . $hosturl .'&key='. $updatekey .'&sysnum='. $sysnum ; $updatenowinfo = file_get_contents($updatehosturl); if (strstr($updatenowinfo, 'zip')){ $pathurl = $updatehost . '?a=down&f=' . $updatenowinfo .'&sysnum='. $sysnum; $updatedir = './Data/logs/Temp/update'; delDirAndFile($updatedir); get_file($pathurl, $updatenowinfo, $updatedir); $updatezip = $updatedir . '/' . $updatenowinfo; $archive = new PclZip($updatezip); if ($archive -> extract(PCLZIP_OPT_PATH, './', PCLZIP_OPT_REPLACE_NEWER) == 0){ $updatenowinfo = "远程升级文件不存在.升级失败</font>"; }else{ $sqlfile = $updatedir . '/update.sql'; $sql = file_get_contents($sqlfile); if($sql){ $sql = str_replace("wy_", C('DB_PREFIX'), $sql); $Model = new Model(); error_reporting(0); foreach(split(";[\r\n]+", $sql) as $v){ @mysql_query($v); } } $updatenowinfo = "<font color=red>升级完成 {$sqlinfo}</font><span><a href=./index.php?g=System&m=Update>点击这里 查看是否还有升级包</a></span>"; } } //delDirAndFile($updatedir); $this -> assign('updatenowinfo', $updatenowinfo); $this -> display(); } } ~~~