PHP动态修改配置文件代码

该文描述了一个简单的PHP系统,用于展示如何通过网页界面修改配置文件。用户在index.php中看到一个表单,表单中的输入字段对应config.php文件中的常量定义。提交表单后,doUpdate.php读取并更新config.php文件,用新输入的值替换旧的常量值,然后重定向回主页。
摘要由CSDN通过智能技术生成

文件结构

index.php 主页

config  配置文件

doUpdate.php 修改功能页

index.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<html>

    <head>

        <title>修改配置</title>

        <meta charset='utf-8' />

    </head>

     

    <body>

        <form action='doUpdate.php' method='post'>

            <table border='1' width='300'>

                <?php                    //读取文件

                    $info=file_get_contents("config.php");                    //var_dump($info);                    

                    //正则

                    preg_match_all('/define("(.*?)","(.*?)")/',$info,$arr);                    //var_dump($arr);                    

                    //遍历

                    foreach($arr[1] 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='2' align='center' >

                        <input type='submit' value='保存' />

                        <input type='reset'  />

                    </td>

                </tr>

            </table>

        </form>

    </body>

</html>

config.php

1

2

3

4

5

<?php

    define("HOST","localhost3311");

    define("USER","root3311");

    define("PWD","1231233311");

    define("DBNAME","test3311");?>

doUpdate.php

1

2

3

4

5

6

7

8

9

<?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");?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执刀人的工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值