Page 1 of 1

[solved]Using php_parse_ini and saving afterwards

Posted: Wed Nov 16, 2005 6:59 am
by jmut
Hi,
I want to create this configurable file. But the configuration will be made from external systems.
So except for parsing the content for using it.....I should also be able to modify it.

I see parse_ini_file parses the file pretty good...and I can easily use such structure of the file.

My question is is there any easy way to modify the data and save the contents back to the file keeping this structure.
Thanks in advance

Posted: Wed Nov 16, 2005 7:27 am
by timvw
I think a websearch for "php ini writer" will return you a couple of classes/functions that can do exactly what you want.

Posted: Wed Nov 16, 2005 7:55 am
by Jenk
look no further than the comments by tomasz.frelik(at)enzo.pl for parse_ini_file()

Code: Select all

function write_ini_file($path, $assoc_array) {

   foreach ($assoc_array as $key => $item) {
       if (is_array($item)) {
           $content .= "\n[$key]\n";
           foreach ($item as $key2 => $item2) {
               $content .= "$key2 = \"$item2\"\n";
           }       
       } else {
           $content .= "$key = \"$item\"\n";
       }
   }       
  
   if (!$handle = fopen($path, 'w')) {
       return false;
   }
   if (!fwrite($handle, $content)) {
       return false;
   }
   fclose($handle);
   return true;
}