douxian1923 2016-09-02 06:52
浏览 275
已采纳

如何将文本文件的数据保存到变量中并在php文件中读取/显示?

I make a website on php and want to read some information from text file. the reason is if, i want to update connection string, data base name, hyperlinks,echo messages etc. then i don't want to update my php code i just replace that text in text file and the relative changes reflects in php file.

For example,

1. A php file :

    <?php
    $servername = "Server_Name";   //Comes from db_config.txt file
    $username = "User_Name";       //Comes from db_config.txt file
    $password = "Password";        //Comes from db_config.txt file

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Conn_Succ_Msg";    //Comes from db_config.txt file
    ?>  

2. A text file like : (db_config.txt)

    Server_Name = localhost
    User_Name = abc
    Password = 123
    Conn_Succ_Msg = Connected successfully

I study "PHP 5 File Open/Read/Close" from http://www.w3schools.com/php/php_file_open.asp but not getting how to implement this even if it possible in xml file, etc also fine. Or can it possible with java scripts, jquery etc. Any idea how to do this ?

  • 写回答

2条回答 默认 最新

  • douxingti9307 2016-09-02 07:02
    关注

    For a simple task like that I would use file() function:

    $config = file("db_config.txt");
    

    So, you will get an array with a cell for each line. Then you can parse:

    foreach ($config as $parameter)
    {
         $ar = explode(" = ", $parameter);
         $$ar[0] = $ar[1];
    }
    

    Note that the assignment of "$$ar[0]" has two $$: this will create a new variable which name will be the content of $ar[0]. This way you will have all your parameters as variables with the same name ($Server_Name, $User_Name, etc).

    Then, your final script:

    $config = file("db_config.txt");
    
    foreach ($config as $parameter)
    {
         $ar = explode(" = ", $parameter);
         $$ar[0] = $ar[1];
    }
    
    // Create connection
    $conn = new mysqli($Server_Name, $User_name, $Password);
    
    // Check connection
    if ($conn->connect_error)
    {
        die("Connection failed: " . $conn->connect_error);
    }
    echo $Conn_Succ_Msg;
    

    Thus, this is not the best way to do it. You should create a .php file with all your parameters defined as PHP variables and then include it, like this:

    example db_config.php:

    $Server_Name = 'localhost';
    $User_Name = 'abc';
    $Password = '123';
    $Conn_Succ_Msg = 'Connected successfully';
    

    Then add at the head of your PHP script:

    include('db_config.php');
    
    // Create connection
    $conn = new mysqli($Server_Name, $User_name, $Password);
    
    // Check connection
    if ($conn->connect_error)
    {
        die("Connection failed: " . $conn->connect_error);
    }
    echo $Conn_Succ_Msg;
    

    This is safer, because you do not risk that someone can access your file directly by URL, if you set wrong file permissions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取