How to dynamically modify configuration files in PHP

不言
Release: 2023-03-31 13:44:02
Original
4503 people have browsed it

This article mainly introduces how to dynamically modify the configuration file in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

A dynamic website generally has various Backend configuration, if the amount of configuration is not large, it would be a waste of resources to design a separate table

Some children like to store various configurations directly in the project. If you want to control the backend, you need a Here’s how to add, delete, modify, and check configuration files.

Let’s not talk nonsense, da~da~da~

Create a new PHP file and name it Config.class.php. Then just use it according to the content in the comments~

/* 
* @link https://mkblog.cn/ 
* @author mengkun 
* @license MIT 
*/  

/** 
 * PHP 无数据库配置文件增删查改模块 
 * !注:本模块未对高并发进行优化兼容,如果数据量或并发过大,还是用数据库比较好 ? 
 *  
 * 使用方法: 
 *  
 * 一、引用本模块 
 *  
 *  require_once 'Config.class.php'; 
 *  
 *  
 * 二、初始化 
 *  
 *  $C = new Config('配置文件名');  // * 如果是在二级目录下,请确保该目录存在 
 *  
 *  
 * 三、内置方法 
 *  
 *  - 存储(如果已存在则是修改)单条数据 
 *  
 *      $C->set('sitename', '哒哒哒'); 
 *  
 *  
 *  - 存储(如果已存在则是修改)一个数组 
 *       
 *      $C->set('user', array('name'=>'peter', 'age'=>12)); 
 *  
 *  
 *  - 读取一条数据 
 *  
 *      $C->get('user', '默认值'); 
 *  
 *  
 *  - 删除一条数据 
 *  
 *      $C->delete('user'); 
 *  
 *  
 *  - 保存对数据的修改 
 *  
 *      $C->save();     // 保存成功返回 true,否则返回失败原因 
 *  
 *   
 * * 注:为了避免频繁地写文件,以上所有对数据的操作都必须调用一次 $C->save(); 才会真正被保存到配置文件中 
 *       建议将所有的数据操作都执行完后再进行存储操作。 
 *  
 *  
 * * 附:精简写法 
 *  
 *      $C->set('sitename', '哒哒哒')->save(); 
 */  

define(&#39;CONFIG_EXIT&#39;, &#39;<?php exit;?>&#39;);  
class Config {  
    private $data;  
    private $file;  

    /** 
     * 构造函数 
     * @param $file 存储数据文件 
     * @return  
     */  
    function __construct($file) {  
        $file = $file.&#39;.php&#39;;  
        $this->file = $file;  
        $this->data= self::read($file);  
    }  

    /** 
     * 读取配置文件 
     * @param $file 要读取的数据文件 
     * @return 读取到的全部数据信息 
     */  
    public function read($file) {  
        if(!file_exists($file)) return array();  

        $str = file_get_contents($file);  
        $str = substr($str, strlen(CONFIG_EXIT));  
        $data = json_decode($str, true);  
        if (is_null($data)) return array();  
        return $data;  
    }  

    /** 
     * 获取指定项的值 
     * @param $key 要获取的项名 
     * @param $default 默认值 
     * @return data 
     */  
    public function get($key = null, $default = &#39;&#39;) {  
        if (is_null($key)) return $this->data;  // 取全部数据  

        if(isset($this->data[$key])) return $this->data[$key];  
        return $default;  
    }  

    /** 
     * 设置指定项的值 
     * @param $key 要设置的项名 
     * @param $value 值 
     * @return null 
     */  
    public function set($key, $value) {  
        if(is_string($key)) {   // 更新单条数据  
            $this->data[$key] = $value;  
        } else if(is_array($key)) {   // 更新多条数据                 
            foreach ($this->data as $k => $v) {  
                if ($v[$key[0]] == $key[1]) {  
                    $this->data[$k][$value[0]] = $value[1];  
                }  
            }  
        }  

        return $this;  
    }  

    /** 
     * 删除并清空指定项 
     * @param $key 删除项名 
     * @return null 
     */  
    public function delete($key) {  
        unset($this->data[$key]);  

        return $this;  
    }  

    /** 
     * 保存配置文件 
     * @param $file 要保存的数据文件 
     * @return true-成功 其它-保存失败原因 
     */  
    public function save() {  
        if(defined(&#39;JSON_PRETTY_PRINT&#39;)) {  
            $jsonStr = json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);  
        } else {  
            $jsonStr = json_encode($this->data);  
        }  

        // 含有二进制或非utf8字符串对应检测  
        if(is_null($jsonStr)) return &#39;数据文件有误&#39;;  
        $buffer = CONFIG_EXIT.$jsonStr;  

        $file_strm = fopen($this->file, &#39;w&#39;);  
        if(!$file_strm) return &#39;写入文件失败,请赋予 &#39;.$file.&#39; 文件写权限!&#39;;  
        fwrite($file_strm, $buffer);  
        fclose($file_strm);  
        return true;  
    }  
}
Copy after login
Copy after login

Code interception source mengken blog

A dynamic website usually has various background configurations. If there is not much configuration, design a separate one The table seems to be a waste of resources

Some children like to store various configurations directly in the project. If they want to control the background, they need a set of operation methods to add, delete, modify and check the configuration files

Let’s not talk nonsense, da~da~da~

Create a new PHP file, name it Config.class.php, and then use it according to the content in the comments~

/* 
* @link https://mkblog.cn/ 
* @author mengkun 
* @license MIT 
*/  

/** 
 * PHP 无数据库配置文件增删查改模块 
 * !注:本模块未对高并发进行优化兼容,如果数据量或并发过大,还是用数据库比较好 ? 
 *  
 * 使用方法: 
 *  
 * 一、引用本模块 
 *  
 *  require_once &#39;Config.class.php&#39;; 
 *  
 *  
 * 二、初始化 
 *  
 *  $C = new Config(&#39;配置文件名&#39;);  // * 如果是在二级目录下,请确保该目录存在 
 *  
 *  
 * 三、内置方法 
 *  
 *  - 存储(如果已存在则是修改)单条数据 
 *  
 *      $C->set(&#39;sitename&#39;, &#39;哒哒哒&#39;); 
 *  
 *  
 *  - 存储(如果已存在则是修改)一个数组 
 *       
 *      $C->set(&#39;user&#39;, array(&#39;name&#39;=>&#39;peter&#39;, &#39;age&#39;=>12)); 
 *  
 *  
 *  - 读取一条数据 
 *  
 *      $C->get(&#39;user&#39;, &#39;默认值&#39;); 
 *  
 *  
 *  - 删除一条数据 
 *  
 *      $C->delete(&#39;user&#39;); 
 *  
 *  
 *  - 保存对数据的修改 
 *  
 *      $C->save();     // 保存成功返回 true,否则返回失败原因 
 *  
 *   
 * * 注:为了避免频繁地写文件,以上所有对数据的操作都必须调用一次 $C->save(); 才会真正被保存到配置文件中 
 *       建议将所有的数据操作都执行完后再进行存储操作。 
 *  
 *  
 * * 附:精简写法 
 *  
 *      $C->set(&#39;sitename&#39;, &#39;哒哒哒&#39;)->save(); 
 */  

define(&#39;CONFIG_EXIT&#39;, &#39;<?php exit;?>&#39;);  
class Config {  
    private $data;  
    private $file;  

    /** 
     * 构造函数 
     * @param $file 存储数据文件 
     * @return  
     */  
    function __construct($file) {  
        $file = $file.&#39;.php&#39;;  
        $this->file = $file;  
        $this->data= self::read($file);  
    }  

    /** 
     * 读取配置文件 
     * @param $file 要读取的数据文件 
     * @return 读取到的全部数据信息 
     */  
    public function read($file) {  
        if(!file_exists($file)) return array();  

        $str = file_get_contents($file);  
        $str = substr($str, strlen(CONFIG_EXIT));  
        $data = json_decode($str, true);  
        if (is_null($data)) return array();  
        return $data;  
    }  

    /** 
     * 获取指定项的值 
     * @param $key 要获取的项名 
     * @param $default 默认值 
     * @return data 
     */  
    public function get($key = null, $default = &#39;&#39;) {  
        if (is_null($key)) return $this->data;  // 取全部数据  

        if(isset($this->data[$key])) return $this->data[$key];  
        return $default;  
    }  

    /** 
     * 设置指定项的值 
     * @param $key 要设置的项名 
     * @param $value 值 
     * @return null 
     */  
    public function set($key, $value) {  
        if(is_string($key)) {   // 更新单条数据  
            $this->data[$key] = $value;  
        } else if(is_array($key)) {   // 更新多条数据                 
            foreach ($this->data as $k => $v) {  
                if ($v[$key[0]] == $key[1]) {  
                    $this->data[$k][$value[0]] = $value[1];  
                }  
            }  
        }  

        return $this;  
    }  

    /** 
     * 删除并清空指定项 
     * @param $key 删除项名 
     * @return null 
     */  
    public function delete($key) {  
        unset($this->data[$key]);  

        return $this;  
    }  

    /** 
     * 保存配置文件 
     * @param $file 要保存的数据文件 
     * @return true-成功 其它-保存失败原因 
     */  
    public function save() {  
        if(defined(&#39;JSON_PRETTY_PRINT&#39;)) {  
            $jsonStr = json_encode($this->data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);  
        } else {  
            $jsonStr = json_encode($this->data);  
        }  

        // 含有二进制或非utf8字符串对应检测  
        if(is_null($jsonStr)) return &#39;数据文件有误&#39;;  
        $buffer = CONFIG_EXIT.$jsonStr;  

        $file_strm = fopen($this->file, &#39;w&#39;);  
        if(!$file_strm) return &#39;写入文件失败,请赋予 &#39;.$file.&#39; 文件写权限!&#39;;  
        fwrite($file_strm, $buffer);  
        fclose($file_strm);  
        return true;  
    }  
}
Copy after login
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

thinkphp common path usage analysis

PHPMailer ThinkPHP realizes the automatic sending email function

The above is the detailed content of How to dynamically modify configuration files in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template