搜索
首页php教程php手册配置文件在正式服务器上的部署

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

配置文件在正式服务器上的部署。
从文件服务器下载配置文件,备份并替换本地的配置文件,将配置文件压缩成zip格式
<?php
header("Content-Type:text/html;charset=utf-8");
include 'tz.php';
/**
 * 执行配置文件的压缩
 * Created by PhpStorm.
 * User: saint
 * Date: 2015/3/18
 * Time: 14:25
 */
if(!defined("SYSTEM_PATH")) 	define("SYSTEM_PATH",	'/data/wwwroot/static/webroot');

class Compress
{
    /**
     * @var array 这里是需要处理的配置文件
     */
    public $cfg_files = array(
        'DEMO1Config.json', #
    );

    /**
     * @var string 文件服务器地址
     */
    public $file_server = '';

    public $cfg_path = '';

    public $temp_path = '';

    public $zip_file = '';

    /**
     * 环境初始化
     */
    public function _InitEnv()
    {
        $this->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 '<p>生成压缩文件成功,本次耗时:' . $limit . 's</p>';
        echo '<p>开始替换原来的配置文件</p>';

        $this->replaceFiles();

        // 插入数据库


        echo '<p>替换完毕</p>';

    }

    // 替换原来的文件
    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();
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境