PHP изменить только одно значение внутри INI-файла - PullRequest
0 голосов
/ 16 апреля 2019

Я пытаюсь переписать файл 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 голосов
/ 16 апреля 2019

Получил это исправить, как это

  $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.'/');
...