当前位置:网站首页>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);边栏推荐
- SPI read / write flash principle + complete code
- 【thingsboard】替换首页logo的方法
- How does the applet solve the rendering layer network layer error?
- Components in protective circuit
- [Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
- FFmepg使用指南
- DFS and BFS concepts of trees and graphs
- Threejs Internet of things, 3D visualization of factory
- provide/inject
- 网络安全-记录web漏洞修复
猜你喜欢

行为感知系统

Function (error prone)

mxnet导入报各种libcudart*.so、 libcuda*.so找不到

蛇形矩阵

Pyqt pyside custom telescopic menu bar sharing (including tutorial)

【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录

Differences among 10 addressing modes

Three level linkage demo of uniapp uview u-picker components

Containerd series - detailed explanation of plugins

如何实现实时音视频聊天功能
随机推荐
open graph协议
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
kubernetes集群之调度系统
Threejs factory model 3DMAX model obj+mtl format, source file download
【thingsboard】替换首页logo的方法
Hexadecimal to octal
TPG x AIDU|AI领军人才招募计划进行中!
这是一个不确定的时代
Fonction (sujette aux erreurs)
[thingsboard] how to replace the homepage logo
[illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
函数(基本:参数,返回值)
[finebi] the process of making custom maps using finebi
A application wakes up B should be a fast method
Realize the attention function of the article in the applet
As soon as I write the code, President Wang talks with me about the pattern all day
MacBook安装postgreSQL+postgis
level17
OWASP top 10 vulnerability Guide (2021)
Network security - record web vulnerability fixes