逻辑:两个网址。一个网址发布新的版本信息,存放升级压缩包。另外一个获取并且比较当前版本并更新。被更新网站我是自己建了个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);
}
}
}