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

如何将文本文件的数据保存到变量中并在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 ?

我在php上创建了一个网站,想要从文本文件中读取一些信息。 原因是,如果,我想更新连接字符串,数据库名称,超链接,回声消息等,那么我不想更新我的PHP代码我只是替换文本文件中的文本,相关的更改反映在PHP文件中。 p>

例如, p>

1。 一个php文件: strong> p>

 &lt;?php 
 $ servername =“Server_Name”;  //来自db_config.txt文件
 $ username =“User_Name”;  //来自db_config.txt文件
 $ password =“Password”;  //来自db_config.txt文件
 
 //创建连接
 $ conn = new mysqli($ servername,$ username,$ password); 
 
 //检查连接
 if($ conn-&gt  ; connect_error){
 die(“Connection failed:”。$ conn-&gt; connect_error); 
} 
 echo“Conn_Succ_Msg”;  //来自db_config.txt文件
?&gt;  
 代码>  PRE> 
 
 

2。 文本文件如:(db_config.txt) strong> p>

  Server_Name = localhost 
 User_Name = abc 
密码= 123 
 Conn_Succ_Msg =已成功连接
   pre> 
 
 

我从 http://www.w3schools.com/php/php_file_open.asp 但是没有得到如何实现这个,即使它可能在xml文件等也很好。 或者可以使用java脚本,jquery等。任何想法如何做到这一点? p> div>

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.

try it :

<?php

$file_handle = fopen("db_config.txt", "r");
$ser = array();
$i = 0;
while (!feof($file_handle)) {
  $ser[$i] = fgets($file_handle);
  $i++;
}
fclose($file_handle);
$servername = $ser[0];
$servername = explode("=", $servername);
$servername = trim($servername[1]);

$username = $ser[1];
$username = explode("=", $username);
$username = trim($username[1]);

$password = $ser[2];
$password = explode("=", $password);
$password = trim($password[1]);

$Conn_Succ_Msg = $ser[3];
$Conn_Succ_Msg = explode("=", $Conn_Succ_Msg);
$Conn_Succ_Msg = trim($Conn_Succ_Msg[1]);

$servername = $servername;
$username = $username;
$password = $password;

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

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo $Conn_Succ_Msg;

?>

open file and get line and expload line with = and trim for remove spaces .

also you can using expload and trim in while :

$file_handle = fopen("db_config.txt", "r");

$ser = array();
$i = 0;
while (!feof($file_handle)) {

    $ser[$i] = fgets($file_handle);
    $sers = explode("=", $ser[$i]);
    $ser[$i] = trim($sers[1]);
    $i++;
}

fclose($file_handle);

$servername = $ser[0];
$username = $ser[1];
$password = $ser[2];

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo $ser[3];