当前位置:网站首页>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);
边栏推荐
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
- 这是一个不确定的时代
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- How does the applet solve the rendering layer network layer error?
- 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?
- Hexadecimal to octal
- Behavior perception system
- Threejs Internet of things, 3D visualization of factory
- The development of mobile IM based on TCP still needs to keep the heartbeat alive
猜你喜欢
Rome chain analysis
How does the applet solve the rendering layer network layer error?
[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)
基于TCP的移动端IM即时通讯开发仍然需要心跳保活
[uniapp] system hot update implementation ideas
[finebi] the process of making custom maps using finebi
Threejs implements labels and displays labels with custom styles
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
首席信息官如何利用业务分析构建业务价值?
Why can't all browsers on my computer open web pages
随机推荐
How does the applet solve the rendering layer network layer error?
Behavior perception system
Wechat applet development process (with mind map)
Common features of ES6
概率论与数理统计考试重点复习路线
Rome链分析
Introduction to RT thread kernel (4) -- clock management
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
防护电路中的元器件
C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
Is "golden nine and silver ten" the best time to find a job? Not necessarily
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?
Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
揭秘技术 Leader 必备的七大清奇脑回路
Function (basic: parameter, return value)
Realize the attention function of the article in the applet
【虚幻引擎UE】运行和启动的区别,常见问题分析
Threejs realizes rain, snow, overcast, sunny, flame
假设检验——《概率论与数理统计》第八章学习笔记