Jump to content

Updating a config.ini file


iPixel

Recommended Posts

So i'm trying to learn how to use INI files for config purposes.

 

I created a config.ini file that looks like so

 

;setup super admin variables

 

[superadmin]

adm_user = super

adm_pass = admin

 

;setup database variables

 

[database]

driver = mysql

db = myDB

user = root

pass = pass

 

And what I ultimately want to do, is present the user with a form asking for the values for these variables.

Once the user fills it out, I want to use php to update the above variables with the new data provided.

 

Here's what i have so far.

<?php
function updateINIfile($Setting, $replace, $INI_PATH)
        {           
            $ini = fopen($INI_PATH,"r+");           
           
            while($Content = fgets($ini))
            {
			if(preg_match("/".$Setting."/", $Content))
				{
					fwrite($ini, $Setting." = ".$replace."\n");
				}
            }

		fclose($ini);
        }

updateINIfile("driver","oracle","config.ini");
?>

 

The problems I'm having are:

[*]Instead of updating, it writes a new line. Correctly but still a new line instead of updating the current one.

[*]For whatever reason, it also overwrites the line directly below driver = mysql with the new line of driver = oracle

[*]It also, eats a part of adm_user = super

 

I've looked through the php manual under parse_ini_file but those samples create new INI files from scratch.

I cant find a solid script that just updates a preexisting INI file.

 

Any help and/or pointers would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/268249-updating-a-configini-file/
Share on other sites

You typically update/create configuration files by writing the whole thing. Read/parse the existing settings into a php array. Set any of the array elements with values/new values, then write the whole config file based on that array of settings.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.