PHP 只更改 ini 文件中的一个值
PHP change only one value inside ini file
我正在尝试重写一个 config.ini 文件,它看起来像这样
dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=2
我想像这样将重新安装值更改为 1
dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=1
我已经写了一些行,但我卡住了,不知道如何只更改一个值
$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.'/');
这样修好了
$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.'/');
我正在尝试重写一个 config.ini 文件,它看起来像这样
dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=2
我想像这样将重新安装值更改为 1
dbhost=localhost
dbname=phonebook
dbuname=root
dbpass=
reinstall=1
我已经写了一些行,但我卡住了,不知道如何只更改一个值
$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.'/');
这样修好了
$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.'/');