开发者

Reading and writing to a config file

开发者 https://www.devze.com 2023-04-11 05:16 出处:网络
I want to code a few PHP pages, which reads and w开发者_StackOverflow中文版rite some settings from a config.php file. The structure of config.php file

I want to code a few PHP pages, which reads and w开发者_StackOverflow中文版rite some settings from a config.php file. The structure of config.php file

config["exp-date"] = '10-09-2011'; 
config["np"] = 3;

I can just include the file and read the settings, but is there any easy way to change settings via a form. I know of fopen and other, but still not getting what to do after opening.

Update: How can I read the whole array at once?


Why don't you use an INI File? PHP has a function to parse an INI File.. I Think Its parse_ini_file();


Say you have the $config var set inside your config.php (btw be careful with the var name, it's very common and you don't want it to be overwrited).

$config = array();
$config['todo'] = 'pony';

All you have to do is to modify the $config array with your form (this part is up to you), and then, write the new $config array in the config.php file like this:

file_put_contents("config.php", var_export($config, true));

And that's it! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号