当前位置:网站首页>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);
边栏推荐
- Ctfshow web entry code audit
- 【thingsboard】替换首页logo的方法
- 【虚幻引擎UE】实现测绘三脚架展开动画制作
- Bit operation skills
- 概率论与数理统计考试重点复习路线
- 蛇形矩阵
- [illusory engine UE] method to realize close-range rotation of operating objects under fuzzy background and pit recording
- Function (basic: parameter, return value)
- Uni app change the default component style
- Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
猜你喜欢
Fonction (sujette aux erreurs)
Interview related high-frequency algorithm test site 3
小程序中实现文章的关注功能
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?
American 5g open ran suffered another major setback, and its attempt to counter China's 5g technology has failed
Rome chain analysis
【FineBI】使用FineBI制作自定义地图过程
How to realize real-time audio and video chat function
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Function (basic: parameter, return value)
随机推荐
概率论与数理统计考试重点复习路线
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
MacBook installation postgresql+postgis
学习MVVM笔记(一)
【虚幻引擎UE】实现测绘三脚架展开动画制作
All in one 1413: determine base
这是一个不确定的时代
如何实现实时音视频聊天功能
Number of possible stack order types of stack order with length n
Uni app common functions /api
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
OWASP top 10 vulnerability Guide (2021)
“金九银十”是找工作的最佳时期吗?那倒未必
Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
TPG x AIDU|AI领军人才招募计划进行中!
长度为n的入栈顺序的可能出栈顺序
Judge whether the stack order is reasonable according to the stack order
MacBook安装postgreSQL+postgis
陇原战“疫“2021网络安全大赛 Web EasyJaba
Phpmailer reported an error: SMTP error: failed to connect to server: (0)