当前位置:网站首页>PHP reads the INI file and writes the modified content
PHP reads the INI file and writes the modified content
2022-07-05 04:22:00 【Casual_ one thousand five hundred and seventy-three】
Read ini File and overwrite data written to ini
updateTime.ini
# Update time record
[fileUpdateTime]
time = ""
[sqlOtherUpdateTime]
time = "2022-06-08"
[sqlUpdateTime]
time = "2022-05-15"
// Method 1
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");
// Method 2
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);
边栏推荐
- Fonction (sujette aux erreurs)
- 函数(基本:参数,返回值)
- 【虚幻引擎UE】运行和启动的区别,常见问题分析
- Use threejs to create geometry and add materials, lights, shadows, animations, and axes
- 小程序中实现文章的关注功能
- Looking back on 2021, looking forward to 2022 | a year between CSDN and me
- Rome链分析
- Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
- User behavior collection platform
- [thingsboard] how to replace the homepage logo
猜你喜欢
如何优雅的获取每个分组的前几条数据
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
Interview related high-frequency algorithm test site 3
“金九银十”是找工作的最佳时期吗?那倒未必
托管式服务网络:云原生时代的应用体系架构进化
【UNIAPP】系统热更新实现思路
Uni app common functions /api
Threejs Internet of things, 3D visualization of factory
Threejs implements labels and displays labels with custom styles
The development of mobile IM based on TCP still needs to keep the heartbeat alive
随机推荐
Machine learning -- neural network
假设检验——《概率论与数理统计》第八章学习笔记
Hexadecimal to octal
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
Introduction to RT thread kernel (5) -- memory management
【科普】热设计基础知识:5G光器件之散热分析
Network security - record web vulnerability fixes
All in one 1413: determine base
Mixed compilation of C and CC
Ctfshow web entry code audit
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
The development of mobile IM based on TCP still needs to keep the heartbeat alive
基于TCP的移动端IM即时通讯开发仍然需要心跳保活
函數(易錯)
Laravel8 export excel file
WGS84 coordinate system, web Mercator, gcj02 coordinate system, bd09 coordinate system - brief introduction to common coordinate systems
电源管理总线 (PMBus)
【thingsboard】替换首页logo的方法
A应用唤醒B应该快速方法
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)