当前位置:网站首页>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);
边栏推荐
- All in one 1413: determine base
- Alibaba cloud ECS uses cloudfs4oss to mount OSS
- The order of LDS links
- PR video clip (project packaging)
- Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
- Introduction to RT thread kernel (4) -- clock management
- Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
- mysql的七种join连接查询
- 机器学习 --- 决策树
- 陇原战“疫“2021网络安全大赛 Web EasyJaba
猜你喜欢
[phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
mysql的七种join连接查询
Fonction (sujette aux erreurs)
蛇形矩阵
OWASP top 10 vulnerability Guide (2021)
kubernetes集群之调度系统
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
北京程序员的真实一天!!!!!
直播預告 | 容器服務 ACK 彈性預測最佳實踐
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
随机推荐
函数(基本:参数,返回值)
Rust blockchain development - signature encryption and private key public key
Behavior perception system
open graph协议
Function (basic: parameter, return value)
Wechat applet development process (with mind map)
机器学习 --- 神经网络
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
机器学习 --- 决策树
Ctfshow 2022 Spring Festival welcome (detailed commentary)
Uni app common functions /api
陇原战“疫“2021网络安全大赛 Web EasyJaba
Un réveil de l'application B devrait être rapide
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
如何进行「小步重构」?
[finebi] the process of making custom maps using finebi
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
这是一个不确定的时代
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
Hexadecimal to octal