当前位置:网站首页>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);边栏推荐
- PR video clip (project packaging)
- 概率论与数理统计考试重点复习路线
- [untitled]
- How to get the first few pieces of data of each group gracefully
- Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
- 长度为n的入栈顺序的可能出栈顺序
- 【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
- Here comes the Lantern Festival red envelope!
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- open graph协议
猜你喜欢

About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture

Alibaba cloud ECS uses cloudfs4oss to mount OSS

网络安全-记录web漏洞修复

Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)

Threejs Internet of things, 3D visualization of farms (I)

NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download

Interview related high-frequency algorithm test site 3

直播預告 | 容器服務 ACK 彈性預測最佳實踐

Function (error prone)
![[thingsboard] how to replace the homepage logo](/img/65/5296c26f975d79d65d36c2e76e47c1.png)
[thingsboard] how to replace the homepage logo
随机推荐
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
Three level linkage demo of uniapp uview u-picker components
陇原战“疫“2021网络安全大赛 Web EasyJaba
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
[untitled]
Learning notes 8
Seven join join queries of MySQL
TPG x AIDU|AI领军人才招募计划进行中!
Behavior perception system
Longyuan war "epidemic" 2021 network security competition web easyjaba
OWASP top 10 vulnerability Guide (2021)
行为感知系统
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
mysql的七种join连接查询
托管式服务网络:云原生时代的应用体系架构进化
Laravel8 export excel file
A solution to the problem that variables cannot change dynamically when debugging in keil5
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
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?
Rust blockchain development - signature encryption and private key public key