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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.