当前位置:网站首页>PHP读取ini文件并修改内容写入
PHP读取ini文件并修改内容写入
2022-07-05 04:21:00 【Casual_1573】
读取ini文件并改写数据写入到ini
updateTime.ini
#更新时间记录
[fileUpdateTime]
time = ""
[sqlOtherUpdateTime]
time = "2022-06-08"
[sqlUpdateTime]
time = "2022-05-15"
//方法一
function write_ini_file($array, $file) {
$res = array();
foreach($array as $key => $val) {
if(is_array($val)) {
$res[] = "[$key]";
foreach($val as $skey => $sval) {
$res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
}
} else {
$res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
}
}
safefilerewrite($file, implode("\r\n", $res));
}
function safefilerewrite($fileName, $dataToSave) {
if ($fp = fopen($fileName, 'w')) {
$startTime = microtime(TRUE);
do {
$canWrite = flock($fp, LOCK_EX);
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
if ($canWrite) {
fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
}
}
$timeInfo = parse_ini_file('updateTime.ini',true);
$timeInfo['sqlOtherUpdateTime']['time'] = date("Y-m-d", time());
write_ini_file($timeInfo, "updateTime.ini");
//方法二
function write_ini_file_tow($assoc_arr, $path, $has_sections=FALSE) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $key=>$elem) {
$content .= "[".$key."]\n";
foreach ($elem as $key2=>$elem2) {
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = \"".$elem2[$i]."\"\n";
}
}
else if($elem2=="") $content .= $key2." = \n";
else $content .= $key2." = \"".$elem2."\"\n";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem) {
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key."[] = \"".$elem[$i]."\"\n";
}
}
else if($elem=="") $content .= $key." = \n";
else $content .= $key." = \"".$elem."\"\n";
}
}
if (!$handle = fopen($path, 'w')) {
return false;
}
$success = fwrite($handle, $content);
fclose($handle);
return $success;
}
$timeInfo = parse_ini_file('updateTime.ini',true);
$timeInfo['sqlOtherUpdateTime']['time'] = date("Y-m-d", time());
write_ini_file_tow($timeInfo, 'updateTime.ini', true);
边栏推荐
- Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
- Threejs Internet of things, 3D visualization of factory
- Interview related high-frequency algorithm test site 3
- [uniapp] system hot update implementation ideas
- Sword finger offer 07 Rebuild binary tree
- Ctfshow 2022 Spring Festival welcome (detailed commentary)
- Threejs Internet of things, 3D visualization of farm (III) model display, track controller setting, model moving along the route, model adding frame, custom style display label, click the model to obt
- MacBook installation postgresql+postgis
- What is the reason why the webrtc protocol video cannot be played on the easycvr platform?
- Sequence diagram of single sign on Certification Center
猜你喜欢
在线文本行固定长度填充工具
As soon as I write the code, President Wang talks with me about the pattern all day
Learning notes 8
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Components in protective circuit
Network security - record web vulnerability fixes
Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
A solution to the problem that variables cannot change dynamically when debugging in keil5
TPG x AIDU|AI领军人才招募计划进行中!
在线SQL转Excel(xls/xlsx)工具
随机推荐
这是一个不确定的时代
在线文本行固定长度填充工具
Kwai, Tiktok, video number, battle content payment
10种寻址方式之间的区别
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
Use threejs to create geometry and add materials, lights, shadows, animations, and axes
函数(易错)
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
Interview related high-frequency algorithm test site 3
[finebi] the process of making custom maps using finebi
蛇形矩阵
假设检验——《概率论与数理统计》第八章学习笔记
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
Machine learning -- neural network
Threejs implements labels and displays labels with custom styles
北京程序员的真实一天!!!!!
Ctfshow 2022 Spring Festival welcome (detailed commentary)
Pyqt5 displays file names and pictures
Uni app common functions /api
陇原战“疫“2021网络安全大赛 Web EasyJaba