Reputation: 10417
I know this question asked multiple times but almost all answer telling save in db
or $this->config->set_item
.
However those answers do not apply for me. I'm not making a website but an open source project and working on the installation script for non technical persons, that will setup every thing for user during installation. For this I need to update config file with user specific settings during installation and form admin panel later, if user need.
Obviously saving in DB doesn't applicable as I even need to write DB credentials in config file through installation script. $this->config->set_item
is also not applicable as it set only for current session and I want to set it permanently.
Right now I'm working on idea of creating config file templates and update variables there. Is it right way to achieve requirements? Again another problem here, if user change some setting from admin panel (say database credentials), changes will not be reflected immediately. Is there any workaround or better solution for that?
Upvotes: 0
Views: 1347
Reputation: 1587
You can just read config files as common files like *.txt, then parse and change what you need. For example, you want to change username for connection to database (I don't check this code, it's just an example):
$out = '';
$pattern = '$db[\'default\'][\'username\']';
$newValue = 'root';
$fileName = "application/config/database.php";
if(file_exists($fileName)) {
$file = fopen($fileName,'r+');
while(!feof($file)) {
$line = fgets($file);
if(strpos($line, $pattern) !== false){
$out = $pattern . " = '". $newValue . "'";
}else{
$out = $line;
}
}
file_put_contents($file, $out);
fclose($file);
}
Upvotes: 3