我在php上创建了一个网站,想要从文本文件中读取一些信息。原因是,如果,我想更新连接字符串,数据库名称,超链接,回声消息等,那么我不想更新我的PHP代码我只是替换文本文件中的文本,相关的更改反映在PHP文件中。
例如,
1。一个php文件:
<?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。文本文件如:(db_config.txt)
Server_Name = localhost
User_Name = abc
Password = 123
Conn_Succ_Msg = Connected successfully
我从http://www.w3schools.com/php/php_file_open.asp学习“PHP 5文件打开/读取/关闭”但是没有得到如何实现它,即使它可能在xml文件中也可以。或者可以使用java脚本,jquery等。任何想法如何做到这一点?
答案 0 :(得分:3)
试一试:
<?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;
?>
打开文件并使用=获取line和expload行,并删除空格。
你也可以使用expload和trim in:
$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];
答案 1 :(得分:2)
对于像这样的简单任务,我会使用file()函数:
$config = file("db_config.txt");
因此,您将获得一个包含每行单元格的数组。然后你可以解析:
foreach ($config as $parameter)
{
$ar = explode(" = ", $parameter);
$$ar[0] = $ar[1];
}
请注意,“$$ ar [0]”的赋值有两个$$:这将创建一个新变量,其名称将是$ ar [0]的内容。这样,您将所有参数都作为具有相同名称的变量($ Server_Name,$ User_Name等)。
然后,你的最终剧本:
$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;
因此,这不是最好的方法。您应该创建一个.php文件,其中所有参数都定义为PHP变量,然后包含它,如下所示:
示例db_config.php:
$Server_Name = 'localhost';
$User_Name = 'abc';
$Password = '123';
$Conn_Succ_Msg = 'Connected successfully';
然后在PHP脚本的头部添加:
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;
这样更安全,因为如果您设置了错误的文件权限,则不会冒任何人直接通过URL访问您的文件的风险。