thinkphp 动态修改config配置文件永久保存

$pat[0] = 'app_debug';
$rep[0] = 'true';

function setconfig($pat, $rep)
    {
        /**
         * 原理就是 打开config配置文件 然后使用正则查找替换 然后在保存文件.
         * 传递的参数为2个数组 前面的为配置 后面的为数值.  正则的匹配为单引号  如果你的是分号 请自行修改为分号
         * $pat[0] = 参数前缀;  例:   app_debug
            $rep[0] = 要替换的内容;    例:  true
         */
        if (is_array($pat) and is_array($rep)) {
            for ($i = 0; $i < count($pat); $i++) {
                $pats[$i] = '/\'' . $pat[$i] . '\'(.*?),/';
                $reps[$i] = "'". $pat[$i]. "'". "=>" . "'".$rep[$i] ."',";
            }
            $fileurl = APP_PATH.request()->module().'/config.php';
            $string = file_get_contents($fileurl); //加载配置文件
            $string = preg_replace($pats, $reps, $string); // 正则查找然后替换
            file_put_contents($fileurl, $string); // 写入配置文件
            return true;
        } else {
            return flase;
        }
    }

 

你可能感兴趣的:(php函数,php类)