thinkphp自定义“更新数据库配置”函数功能

更新数据库配置, (事例基于thinkphp5.0);

亲测可用!

//更新数据库配置
    public function update_dbconfig()
    {   
        $dbConfig = [
            'type'            => 'mysql',
            'hostname'        => 'localhost',
            'database'        => 'laofan',
            'username'        => 'root',
            'password'        => 'root',
            'hostport'        => '3306',
        ];
           try {
                //读取系统配置内容
                $dbconfig_path = APP_PATH . 'database.php';
                $site_conf = htmlentities(file_get_contents($dbconfig_path));
                //替换配置项
                foreach ($dbConfig as $key => $value) {
                    $pats = '/\'' . $key . '\'(.*?)\',/';
                    $reps = "'". $key. "'". "           => " . "'".$value ."',";
                    $site_conf = preg_replace($pats, $reps, $site_conf); // 正则查找然后替换
                }
                if (!file_exists($dbconfig_path)) {
                    mkdir($dbconfig_path, 0777, true);
                }
                $result = file_put_contents($dbconfig_path, stripslashes(htmlspecialchars_decode($site_conf)));    
            } catch (\Exception $e) {
                return json(['staus'=>'1','message'=>'配置文件更新失败']); 
            }
            return json(['staus'=>'2','message'=>'配置文件更新完成']); 
      
    }

 

    A+
发布日期:2020年02月14日 13:06:27  所属分类:ThinkPHP框架
最后更新时间:2020-02-14 13:06:27
标签:
头像
  • ¥ 119.0元
  • 市场价:199.0元
  • ¥ 29.99元
  • 市场价:888元
  • ¥ 68.0元
  • 市场价:168.0元
  • ¥ 388.0元
  • 市场价:388.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:4   其中:访客  0   博主  0

  1. 头像 111 2

    学习PHP

    • 付杰 付杰

      @111 百度网盘共享链接:https://pan.baidu.com/s/4eUPK354

  2. 头像 111 2

    学习PHP全套

    • 付杰 付杰

      @111 百度网盘共享链接:https://pan.baidu.com/s/4eUPK354