当前位置:网站首页>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);
边栏推荐
- Threejs rendering obj+mtl model source code, 3D factory model
- [phantom engine UE] package error appears! Solutions to findpin errors
- 如何实现实时音视频聊天功能
- Threejs factory model 3DMAX model obj+mtl format, source file download
- Fuel consumption calculator
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- Threejs implements labels and displays labels with custom styles
- 【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
- 【UNIAPP】系统热更新实现思路
- Uni app change the default component style
猜你喜欢
Uni app change the default component style
[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)
MacBook installation postgresql+postgis
kubernetes集群之调度系统
How to realize real-time audio and video chat function
如何实现实时音视频聊天功能
Threejs implements labels and displays labels with custom styles
Components in protective circuit
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
行为感知系统
随机推荐
Fonction (sujette aux erreurs)
Use Firefox browser to quickly pick up Web image materials
User behavior collection platform
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?
Components in protective circuit
web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
电源管理总线 (PMBus)
A应用唤醒B应该快速方法
[thingsboard] how to replace the homepage logo
Mixed compilation of C and CC
Uni app common functions /api
Pyqt pyside custom telescopic menu bar sharing (including tutorial)
Ctfshow web entry code audit
【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
WGS84 coordinate system, web Mercator, gcj02 coordinate system, bd09 coordinate system - brief introduction to common coordinate systems
Seven join join queries of MySQL
Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
陇原战“疫“2021网络安全大赛 Web EasyJaba
How to realize real-time audio and video chat function