PHP在ini文件中仅更改一个值

时间:2019-04-16 13:03:28

标签: php ini

我正在尝试重写如下所示的config.ini文件

dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=2

我想像这样将重新安装值更改为1

dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=1

我已经写了几行,但是我被卡住了,不知道如何只更改一个值

    $filepath = 'config.ini';

            $data = @parse_ini_file("config.ini");;
            //update ini file, call function
            function update_ini_file($data, $filepath) {
              $content = "";
              //parse the ini file to get the sections
              //parse the ini file using default parse_ini_file() PHP function
              $parsed_ini = parse_ini_file($filepath, true);
              foreach($data as $section => $values){
                if($section === "submit"){
                  continue;
                }
                $content .= $section ."=". $values . "\n";
              }
              //write it into file
              if (!$handle = fopen($filepath, 'w')) {
                return false;
              }
              $success = fwrite($handle, $content);
              fclose($handle);
            }
            update_ini_file($data, $filepath);
            header('location: '.ROOT_PATH.'/');

1 个答案:

答案 0 :(得分:0)

像这样修复

  $filepath = 'config.ini';

            $data = @parse_ini_file("config.ini");
             $data['reinstall']='1';
            //update ini file, call function
            function update_ini_file($data, $filepath) {
              $content = "";
              //parse the ini file to get the sections
              //parse the ini file using default parse_ini_file() PHP function
              $parsed_ini = parse_ini_file($filepath, true);
              foreach($data as $section => $values){
                if($section === "submit"){
                  continue;
                }
                $content .= $section ."=". $values . "\n";
              }
              //write it into file
              if (!$handle = fopen($filepath, 'w')) {
                return false;
              }
              $success = fwrite($handle, $content);
              fclose($handle);
            }
            update_ini_file($data, $filepath);
            header('location: '.ROOT_PATH.'/');