问答项目---金币经验奖励规则及网站配置写入config文件
具体步骤:
引入配置文件——>获取当前数组——>进行合并
public function edit(){ //引入 config.php配置文件 $file = APP_PATH.'Common/Conf/config.php'; $config = include APP_PATH.'Common/Conf/config.php'; $date = I('post.'); $date = array_change_key_case($date,CASE_UPPER); //合并 $config = array_merge($config,$date); //重写 config 文件 //由于是数组,在进行写入的时候,需要转换为字符串 $str = "<?php\r\nreturn ".var_export($config,true).";\r\n?>"; if(file_put_contents($file,$str)){ //从哪里来回到哪里去 $this->success('修改成功',$_SERVER['HTTP_REFERER']); }else{ $this->error('修改失败'); }; //p($config); }
Thinkphp6 配置:
public function add() { $file = '../config/site.php'; $config = config('site'); $config = array_change_key_case($config,CASE_UPPER); $site = ['bb' => '996']; $site = array_change_key_case($site,CASE_UPPER); // 合并 $merge = array_merge($config,$site); // 重写 $str = "<?php\r\nreturn ".var_export($merge,true).";\r\n?>"; $result = file_put_contents($file,$str); return $result ? $this->success() : $this->error(); }