iPixel Posted September 11, 2012 Share Posted September 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268249-updating-a-configini-file/ Share on other sites More sharing options...
scootstah Posted September 11, 2012 Share Posted September 11, 2012 You may be happy to know that parse_ini_file exists. Quote Link to comment https://forums.phpfreaks.com/topic/268249-updating-a-configini-file/#findComment-1376922 Share on other sites More sharing options...
iPixel Posted September 11, 2012 Author Share Posted September 11, 2012 Im aware of it's existence. However, parsing is not my issue. I can read the INI file just fine. My issue is with updating specific variables within the INI file. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/268249-updating-a-configini-file/#findComment-1377005 Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2012 Share Posted September 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268249-updating-a-configini-file/#findComment-1377008 Share on other sites More sharing options...
iPixel Posted September 11, 2012 Author Share Posted September 11, 2012 Hah!, that actually makes sense. Sucks but makes sense. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/268249-updating-a-configini-file/#findComment-1377020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.