PHP在线检测是否更新并解压覆盖代码
<?php
error_reporting(0);
ob_implicit_flush(true);
$begin = microtime(true);
//检查更新,加载本地版本号
$config = require(__DIR__ . '/config.php');
$server = 'https://www.bang4.cn'; //你的服务器地址
ob_flush();
$versions = json_decode(file_get_contents($server), true);
if (!isset($versions[$config['version']])) {
echo '当前已是最新版本!';
} else {
echo '当前版本:<b>', $config['version'], '</b> 服务器版本:<b>',
$versions[$config['version']]['version'], '</b><br/>';
ob_flush();
//开始下载
$remote_fp = fopen($versions[$config['version']]['url'], 'rb');
if(!is_dir(__DIR__.'/tmp')) mkdir(__DIR__.'/tmp');
$tmp = __DIR__ . '/tmp/' . date('YmdHis') . '.zip';
$local_fp = fopen($tmp, 'wb');
echo '开始下载...<br/>';
ob_flush();
while (!feof($remote_fp)) {
fwrite($local_fp, fread($remote_fp, 512));
}
fclose($remote_fp);
fclose($local_fp);
echo '下载完成,准备解压<br/>';
ob_flush();
require(__DIR__ . '/Zip.php');
$zip = new Zip();
$zip->extra($tmp, __DIR__);
echo '解压完成,准备删除临时文件<br/>';
ob_flush();
//删除补丁包
unlink($tmp);
echo '临时文件删除完毕<br/>';
ob_flush();
//更新本地版本号
$content = file_get_contents(__DIR__.'/config.php');
$content = str_replace($config['version'],$versions[$config['version']]['version'],$content);
file_put_contents(__DIR__.'/config.php',$content);
echo '更新完成!耗时',microtime(true) - $begin,'秒<br/>';
ob_flush();
ob_end_clean();
exit();
}