php教程

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

我的站长站 2022-08-12 人阅读

本教程是非常经典的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' = 'wdzzz.com';
'm' = 'wdzzz.com';
php;
相关推荐
  • php修改文件
  • php配置
  • PHP批量修改数据库表前缀教程+代码

    有时候需要批量修改数据库表前缀,最简单的方法就是一个表一个表的手动修改,但是非常浪费时间。这里为大家分享一段自用的利用PHP批量修改数据库表前缀的方法,亲测可用,下面上代码。PHP修改表前缀代码<?php$dbserver=&#39;localhost&#39;;//连接的服务器...

    php教程 101 4年前
  • PHP同步修改配置文件示例教程+源码

    本教程是非常经典的PHP同步修改配置文件示例教程,非常适合新手朋友学习。主要学习了file_get_contents函数的读写方法,file_get_contents函数读取文件的方法,示例:$info=file_get_contents("文件路径");file_put_contents函数写入内容的方法,示例:file_put...

    php教程 34 1年前
  • PHP修改文件内容示例教程

    HTML提交表单代码:<form name="form2" method="get" action="index.php" id="form2" onSubmit="return confirm(&#39;确认要操作?&#39;);"> <table width="100%" border="0" align="cente...

    php教程 20 1年前
  • php上传大文件必备配置方法

    项目要求如果你的项目需要用到大文件上传或下载功能,就必须首先修改PHP的配置才行,否则上传或下载操作就会超时,操作失败。操作步骤打开php配置文件php.ini,首先找到; file uploads ;区域,有影响文件上传的以下几个参数:file_uploads = on ;//是否允许...

    php教程 50 1年前
  • PHP动态修改配置文件代码

    文件结构index.php 主页config  配置文件doUpdate.php 修改功能页index.php<html> <head> <title>修改配置</title> <meta charset=&#39;utf-8&#39; /> </head> <body> <form action=&#39;doUpdate....

    php教程 25 1年前
  • PHP同步修改配置文件示例教程+源码

    本教程是非常经典的PHP同步修改配置文件示例教程,非常适合新手朋友学习。主要学习了file_get_contents函数的读写方法,file_get_contents函数读取文件的方法,示例:$info=file_get_contents("文件路径");file_put_contents函数写入内容的方法,示例:file_put...

    php教程 34 1年前
最新更新