How to update an ini file with php?

前端 未结 3 1410
北荒
北荒 2020-12-06 19:38

I have an existing ini file that I have created and I would like to know if there was a way to update a section of the file or do I have have to rewrite the entire file each

3条回答
  •  旧巷少年郎
    2020-12-06 19:58

    I used the first suggestion of you:

    So would I use the parse_ini_file to get all of the infromation then make my changes and use fwrite to rewrite the whole file

    function config_set($config_file, $section, $key, $value) {
        $config_data = parse_ini_file($config_file, true);
        $config_data[$section][$key] = $value;
        $new_content = '';
        foreach ($config_data as $section => $section_content) {
            $section_content = array_map(function($value, $key) {
                return "$key=$value";
            }, array_values($section_content), array_keys($section_content));
            $section_content = implode("\n", $section_content);
            $new_content .= "[$section]\n$section_content\n";
        }
        file_put_contents($config_file, $new_content);
    }
    

提交回复
热议问题