搜索您想要的文章


PHP同步修改配置文件示例教程 源码

作者:Admin采集插件 时间:2023-04-08 浏览:646 分类:php教程 来源:互联网收录

本教程是非常经典的PHP同步修改配置文件示例教程,非常适合新手朋友学习。

主要学习了file_get_contents函数的读写方法,

file_get_contents函数读取文件的方法,示例:$info=file_get_contents("文件路径");

file_put_contents函数写入内容的方法,示例:file_put_contents("文件路径",写入内容变量);

文件结构:

index.php 主页,显示和表单提交

config  配置文件,储存数据

doUpdate.php 修改处理

index.php代码:

<html>
   <head>
       <title>修改配置</title>
       <meta charset='utf-8' />
   </head>
 
   <body>
       <form action='doUpdate.php' method='post'>
           <table border='' width=''>
               <?php
                   //读取文件
                   $info=file_get_contents("config.php");
                   //var_dump($info);
 
                   //正则
                   preg_match_all('/define("(.*?)","(.*?)")/',$info,$arr);
                   //var_dump($arr);
 
                   //遍历
                   foreach($arr[] as $k=>$v){
                       echo "<tr>";
                           echo "<td>{$v}</td>";
                           echo "<td><input type='text' name='{$v}' value='{$arr[2][$k]}' /></td>";
                       echo "</tr>";
                   }
               ?>
               <tr>
                   <td colspan='' align='center' >
                       <input type='submit' value='保存' />
                       <input type='reset'  />
                   </td>
               </tr>
           </table>
       </form>
   </body>
</html>

config.php代码:

<?php
   define("HOST","localhost3311");
   define("USER","root3311");
   define("PWD","");
   define("DBNAME","test3311");
?>

doUpdate.php代码:

<?php
   //读文件
   $info=file_get_contents("config.php");
 
   //var_dump($_POST);
   //die;
   //遍历$_POST
   foreach($_POST as $k=>$v){
       //正则替换
       $info=preg_replace("/define("{$k}",".*?")/","define("{$k}","{$v}")",$info);
   }
 
   //回填
   file_put_contents("config.php",$info);
   echo "ok";
   header("refresh:1;url=index.php");
 
?>

以上案列用到了正则匹配的方法修改指定内容,不会正则的可以使用<<<EOF 和 EOF的方法直接修改整个文件的内容。

$newdata = <<<php
<?php
'www' = 'zidongcaiji.com';
'm' = 'zidongcaiji.com';
php;

本文章来自互联网,有问题请联系QQ:1739635


免责声明:1.凡本站注明“来源:XXX(非CMS自动采集助手)”的作品,均转载自其它媒体,所载的文章、图片、音频视频文件等资料的版权归版权所有人所有,如有侵权,请联系1321198484@qq.com处理;2.凡本站转载内容仅代表作者本人观点,与CMS自动采集助手无关。