ThinkPHP修改配置文件参数值
随风飘扬 php 2017年07月14日20:06 5256
由于想写一个后台管理,可以对系统数据库配置等conf.php内的内容进行修改,由于TP自带的C方法仅
由于想写一个后台管理,可以对系统数据库配置等conf.php内的内容进行修改,由于TP自带的C方法仅仅是动态修改,并没有把修改后的配置存储下来,因此萌生了此想法。
虎mv泰et克c新ecz闻eae网xhps版权所有,请勿未授权转载!!!
思路就是读取将传过来的值foreach生成conf.php文件内数据格式写入edit.php,然后在conf.php内'LOAD_EXT_CONFIG' => 'config'即可,下面为代码。
function updateConfig($name=null, $value=null) {
$path = APP_PATH.'/'.MODULE_NAME .'/Conf/edit.php';
$config = include $path;
$edit = is_array($name) ? $name : array("$name" => "$value");//自动识别批量修改还是单个
$res = array_merge($config, $edit);
$str = "<?php \r\n\treturn array(\r\n";
foreach ($res as $key => $value){
$str .= "\t\t".'\''.$key.'\''."\t=>\t".'\''.$value.'\''.','."\r\n";
};
$str .= "\t);\r\n?>";
return file_put_contents($path, $str) ? true : false;
}其中name参数可传入数组和字符串,传入字符串则会读取value参数的值生成一个key-value组成的数组
Declaración: los textos que se reproducen en la red, como textos / gráficos, tienen por objeto transmitir más información y facilitar las deliberaciones, y no implican que el sitio y los patrocinadores o patrocinadores compartan sus opiniones o confirmen la autenticidad de su contenido, que sólo es de carácter informativo y que si se violan los derechos de los autores originales, por favor se pongan en contacto con el Administrador del sitio web.
Lista de comentarios
-
Datos no disponibles