Jump to content

[SOLVED] How to update an ini-like file?


julia k

Recommended Posts

Hi there! My first post here (the reason I joined your forum!) hehe :)

 

My problem is that I have set my application's settings in an ini file. I can get and write new values in it without problems only that I cannot update any existing settings :(

 

anyone can help me with this? please? :)

 

thank you in advance!

Link to comment
Share on other sites

Hi Matthew! :) haha! well...some do :)here is the function that I use to write new values in the file:

function write_ini($sSection='app_settings', $sKey, $sValue){    global $settings_file;    $sFileHandle = fopen($settings_file, "r");    if (!$sFileHandle) {        die("<b>Error:</b> Unable to open ".$settings_file.".");    }    if (filesize($settings_file) > 0) {        $sFileContents = fread($sFileHandle, filesize($settings_file));    }    if ($sFileContents === FALSE) {        die("<b>Error:</b> Unable to read data from ".$settings_file.".");    }    $sFileData = preg_split("/ ? /", $sFileContents);    $sectionStart = -1;    $sectionEnd = -1;    $returnLine = -1;    for ($i = 0; $i < count($sFileData); $i++)    {        $x = strpos($sFileData[$i], "[".$sSection."]");        $y = strpos($sFileData[$i], "[");        if ($y == 0 && $y !== FALSE && $sectionStart >= 0 && $sectionEnd == -1) {            $sectionEnd = ($i - 1);        }        $z = explode("=", $sFileData[$i]);        if (count($z) >= 2) {            if ($z[0] == $sKey && $sectionStart >= 0 && $sectionEnd == -1 && substr($sFileData[$i], 0, 1) != ";") {                $returnLine = $i;            }        }        if ($x == 0 && $x !== FALSE && $sectionStart == -1) {            $sectionStart = ($i + 1);        }    }    if ($sectionEnd == -1) {        $sectionEnd = (count($sFileData)-1);    }    $newfile = "";    if ($returnLine > -1) {        $sFileData[$returnLine] = $sKey."=".$sValue;        for ($i = 0; $i < count($sFileData); $i++) {            $newfile .= $sFileData[$i]." ";        }    }    else {        if ($sectionStart > -1) {            for ($i = 0; $i < count($sFileData); $i++)            {                if ($i == $sectionEnd) {                    $newfile .= $sKey."=".$sValue." ";                }                $newfile .= $sFileData[$i]." ";            }        }        else        {            for ($i = 0; $i < count($sFileData); $i++) {                $newfile .= $sFileData[$i]." ";            }            $newfile .= "[".$sSection."] ".$sKey."=".$sValue." ";        }    }    fclose($sFileHandle);    $sFileHandle = fopen($settings_file, "w");    if (!$sFileHandle) {        die("<b>Error:</b> Unable to open file ".$settings_file." with write access.");    }    $sFileWrite = fwrite($sFileHandle, trim($newfile));    if ($sFileWrite === FALSE) {        die("<b>Error:</b> Unable to write data to file ".$settings_file.".");    }}

to get the values, I use the

$ini = @parse_ini_file($settings_file/*[,true]*/);

where $ini would be an array, you know...For the ini file I use the same format as the one used in the php's ini file...

 

whoa! this editor is pretty messy :(

Link to comment
Share on other sites

To be honest, I never used the serialize and unserialize functions 'til now. I've ran a simple test and, yes, this would be my chosen way :)thank you very much, aximbigfan.

see you guys around!

 

:) [/]

 

Sure! This is the best way I have found. It also allows me to mod a bunch of keys at once, plus, 3d arrays!

 

Chris

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.