Majid Fouladpour
Majid Fouladpour

Reputation: 30252

PHP - Mirror image of what parse_ini_file does?

parse_ini_file could be used to get configuration values from an ini file to use in the code, suppose your app changes some of those values and you want to set new defaults, is there a function to take the associative array and write the ini file over?

Upvotes: 0

Views: 196

Answers (2)

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

I don't remember having ever seen such function bundled with PHP...


But, to avoid re-inventing the wheel, maybe you could take a look at those two components of Zend Framework :

With a bit of luck, those might be easy to extract from the framework and use in your application... it's the case for some components, so maybe it is for those two...

Upvotes: 0

Gordon
Gordon

Reputation: 317119

You could use Zend Framework's Zend_Config_Writer_Ini to write the Ini File back to file. Because Zend Framework is component library, you can use only this component without having to migrate your entire application to ZF.

If you don't mind the config not being an Ini but PHP arrays, you can also use this code to load and write back the configs:

function config_read($filename)
{
    $config = include $filename;
    return $config;
}
function config_write($filename, array $config)
{
    $config = var_export($config, true);
    file_put_contents($filename, "<?php return $config ;");
}

Somewhat related question and my answer to it:

Upvotes: 1

Related Questions