Reputation: 1485
I'm trying to rewrite a config.ini file which looks like this
dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=2
I want to change the reinstall value to 1 like so
dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=1
I already wrote some lines, yet i am stuck and don't know how to change only one value
$filepath = 'config.ini';
$data = @parse_ini_file("config.ini");;
//update ini file, call function
function update_ini_file($data, $filepath) {
$content = "";
//parse the ini file to get the sections
//parse the ini file using default parse_ini_file() PHP function
$parsed_ini = parse_ini_file($filepath, true);
foreach($data as $section => $values){
if($section === "submit"){
continue;
}
$content .= $section ."=". $values . "\n";
}
//write it into file
if (!$handle = fopen($filepath, 'w')) {
return false;
}
$success = fwrite($handle, $content);
fclose($handle);
}
update_ini_file($data, $filepath);
header('location: '.ROOT_PATH.'/');
Upvotes: 1
Views: 877
Reputation: 1485
Got it fixed like this
$filepath = 'config.ini';
$data = @parse_ini_file("config.ini");
$data['reinstall']='1';
//update ini file, call function
function update_ini_file($data, $filepath) {
$content = "";
//parse the ini file to get the sections
//parse the ini file using default parse_ini_file() PHP function
$parsed_ini = parse_ini_file($filepath, true);
foreach($data as $section => $values){
if($section === "submit"){
continue;
}
$content .= $section ."=". $values . "\n";
}
//write it into file
if (!$handle = fopen($filepath, 'w')) {
return false;
}
$success = fwrite($handle, $content);
fclose($handle);
}
update_ini_file($data, $filepath);
header('location: '.ROOT_PATH.'/');
Upvotes: 2