php各种服务器部署文档,配置文件在正式服务器上的部署

配置文件在正式服务器上的部署。 从文件服务器下载配置文件,备份并替换本地的配置文件,将配置文件压缩成zip格式 无 ?phpheader("Content-Type:text/html;charset=utf-8");include 'tz.php';/** * 执行配置文件的压缩 * Created by PhpStorm. * User: saint

配置文件在正式服务器上的部署。

从文件服务器下载配置文件,备份并替换本地的配置文件,将配置文件压缩成zip格式

cfg_path = SYSTEM_PATH . '/metafile';

if(false == (is_dir( $this->cfg_path) && is_writable($this->cfg_path))) {

throw new Exception('配置目录不存在或者不可写');

}

$this->temp_path = SYSTEM_PATH . '/tmp';

if(false == (is_dir($this->temp_path) && is_writable($this->temp_path))) {

throw new Exception('临时配置目录不存在或者不可写');

}

if(function_exists('file_get_contents') == false) {

throw new Exception('file_get_contents函数已禁用');

}

if(function_exists('file_put_contents') == false) {

throw new Exception('file_put_contents函数已禁用');

}

if(class_exists('ZipArchive') == false) {

throw new Exception('找不到ZipArchive');

}

}

public function downloadFiles()

{

$start = microtime(true);

try {

$this->_InitEnv();

} catch (Exception $e) {

echo $e->getMessage();

exit;

}

$file_array = array();

// 依次从服务器上下载配置文件

foreach($this->cfg_files as $filename) {

$tmp_file = $this->temp_path . '/' . $filename;

$remote_file = $this->file_server . '/' . $filename;

// 检查本地是否存在临时文件,如果有,则检查这个文件本地文件是否一致

$is_download = true;

if(is_file($tmp_file)) {

if(hash_file('sha256', $tmp_file) == hash_file('sha256', $remote_file)) {

$is_download = false;

}

}

if($is_download) {

$contents = file_get_contents($remote_file);

file_put_contents($tmp_file, $contents);

}

$file_array[] = $tmp_file;

}

// 对配置文件进行压缩

try {

$this->compressFiles();

} catch (Exception $e) {

echo $e->getMessage();

exit;

}

$limit = microtime(true) - $start;

echo '

生成压缩文件成功,本次耗时:' . $limit . 's';

echo '

开始替换原来的配置文件';

$this->replaceFiles();

// 插入数据库

echo '

替换完毕';

}

// 替换原来的文件

public function replaceFiles()

{

// 打开配置目录,创建备份目录

chdir($this->cfg_path);

$backup_dir = 'cfg_bak_' . date('Ymd');

if(is_dir($backup_dir)) {

rmdir($backup_dir);

}

mkdir($backup_dir, 0777);

array_push($this->cfg_files, $this->zip_file);

foreach($this->cfg_files as $file_name) {

$dst_file = $backup_dir . '/' . $file_name;

if(is_file($file_name)) {

copy($file_name, $dst_file);

}

$new_file = $this->temp_path . '/' . $file_name;

copy($new_file, $file_name);

}

}

/**

* 执行压缩文件

* @param $file_array

* @throws Exception

*/

public function compressFiles()

{

chdir($this->temp_path);

$this->zip_file = 'cfg_' . date(('Ymd')) . '.zip';

$zipClass = new ZipArchive();

$fp = $zipClass->open($this->zip_file, ZipArchive::CREATE);

if($fp === true) {

foreach($this->cfg_files as $file_name) {

$zipClass->addFile($file_name);

}

} else {

throw new Exception('压缩失败:' . $fp);

}

$zipClass->close();

}

}

$obj = new Compress();

$obj->downloadFiles();

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值