### **全新示例**
~~~
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();
}
}
~~~