浮叶蓝空的博客
分享学习心得,记录学习笔记
文章:
84
访问:
525918
登录
关于
首页
今天是:
2025年09月09日 星期二
类别
PHP(39)
其他笔记(20)
Java(1)
C++(17)
mysql(2)
JavaScript(4)
近期文章
Linux下Cmake引用第三方静态库示例
Linux下使用Cmake构建静态库示例
Ubuntu设置自动挂载硬盘,挂载U盘
压力测试工具Apache JMeter基本使用
使用PHP脚本借助FFmpeg一键合并视频
Shell中的>、1>、2>、2>&1、&>详解
PHP开启多进程实现异步非阻塞并行执行任务
博主推荐
PHP实现文件下载接口
Qt读写注册表,C++读写注册表
PHP动态修改配置文件,存储为文件
MySql常用语法
Powershell远程连接
PHP从字符串中获取需要的内容部分
封装PHP的HTTP请求
PHP动态修改配置文件,存储为文件
fuyelk
2019年03月24日
4922
#### PHP动态修改配置文件,并永久存储为文件 ```php /** * 修改配置文件,仅支持修改config目录下的配置文件 * @param string $confFileName 配置文件名,不带扩展名,如database * @param array $newConfig 包含新配置信息的数组,该数组的键必须是原配置文件中有的,原来不存在的项不生效 */ function config_set($confFileName = "",$newConfig = []) { $configPath = APP_ROOT.DS.'config'.DS.$confFileName.'.php'; $strConfigFile = file_get_contents($configPath); // 读取原配置文件 // 最后一个']'的位置 $indexEnd = strrpos($strConfigFile,']'); // return 的位置 $indexReturn = strpos($strConfigFile,'return'); // 第一个'['的位置 $indexStart = $indexReturn+strpos(substr($strConfigFile,$indexReturn),'['); $fileHead = substr($strConfigFile,0,$indexStart+1); $fileFoot = substr($strConfigFile,$indexEnd); // 中间部分以换行符拆分为数组 $strCont = substr($strConfigFile,$indexStart+1,($indexEnd-$indexStart-1)); // 创建副本 $tempConfFile = APP_ROOT.DS.'config'.DS.'temp_conf_'.time().'.tmp'; $fp = fopen($tempConfFile,'w'); fwrite($fp,$strCont); fclose($fp); // 从副本中分析文件内容 $fileopen = fopen($tempConfFile,'r'); $newConfFileData = []; while(!feof($fileopen)){ $eachline = fgets($fileopen); if ('/'===substr(trim($eachline),0,1)){ $newConfFileData[] = $eachline; }else{ // 取''中的内容 $index_key_start = strpos($eachline,"'"); if (false!==$index_key_start){ $index_key_end = strpos($eachline,"'",$index_key_start+1); $itemkey = substr($eachline,$index_key_start+1,$index_key_end-$index_key_start-1); if (!empty($newConfig[$itemkey])){ $newConfFileData[] = substr($eachline,0,$index_key_start)."'".$itemkey."' => '".$newConfig[$itemkey]."',\r\n"; }else{ $newConfFileData[] = $eachline; } } } } fclose($fileopen); unlink($tempConfFile); // 分析完成,删除副本文件 // 创建新文件 $newConfFile = APP_ROOT.DS.'config'.DS.'temp_conf_'.time().'.conf'; $fp = fopen($newConfFile,'w'); fwrite($fp,$fileHead."\r\n".implode('',$newConfFileData).$fileFoot); fclose($fp); if (file_exists($newConfFile)&&file_exists($configPath)){ unlink($configPath); rename($newConfFile,$configPath); }else{ exit("生成配置文件识别"); } } ```
上一篇:
strtotime轻松获取时间
下一篇:
Mysql支持emoji
0人点赞
天蓝色的彼岸
牛*
2019-04-07 21:03:43
# 1
登录后评论
友情链接
doywb
2018-2025 Copyright© 米灵尔 浮叶蓝空
豫ICP备15007436号-1
豫公网安备 41152302000146号