Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an ini file with php?

Tags:

file

php

fwrite

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 time?

Here is an example of my config.ini file:

[config]
    title='test'
    status=0
[positions]
    top=true
    sidebar=true
    content=true
    footer=false

Say I want to change the [positions] top=false. 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. Or is there a way just to change that section?

like image 468
WAC0020 Avatar asked Aug 12 '10 23:08

WAC0020


People also ask

How do I update my config ini file?

Open the Backup Manager installation folder. Find the config. ini file inside (see the table below for the exact file path on each operating system) and open as an Administrator. Edit the settings as needed, save the changes and close the file.

Where is PHP ini file in PHP?

ini file: Whenever we install PHP, we can locate the configuration file inside the PHP folder. If using xampp, we can find the configuration file in one or many versions, inside the path '\xampp\php'. Note: Other versions of this file are php. ini-development and php.

Where is PHP ini in file manager?

On systems that run EasyApache 3, the /usr/local/lib/ directory contains your server's php. ini file.


1 Answers

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);
}
like image 135
mtoloo Avatar answered Nov 01 '22 16:11

mtoloo