我正在构建的 Web 应用程序中有一个config.inc
文件。它包含一个数组,其中包含 MySQL 数据库等的配置值。我希望使用简单的表单输入这些值,询问服务器、数据库的登录名/密码等,然后将这些写入配置文件。
有这样做的首选方法吗?我不确定如何写入文件并更新数组。
你只是想写作,对吗?它是序列化数组还是已解析?
读取配置文件的一种方法是parse_ini_file()。我不一定称它为首选,但它是一种方法。您仍然需要编写文件。
另一种方法是编写“config.inc.php”并将其包含在内,要编写它,您只需输出实际的 PHP 代码(例如 $var = “myval”;)。
这是一种您可以编写一个简单的“输出”函数的方法,该函数接受一个配置值数组并将它们输出为 name=value,假设 $config 是一个关联数组。
foreach ($config as $name => $value) {
$output .= $name . '=' . $value . "\n";
}
if (!file_put_contents($filename, $output)) {
die("Error writing config file.");
}
有很多不错的方法可以做到这一点。这真的取决于你的要求。它需要采用特定格式还是您有余地?
不建议通过您的应用程序修改 PHP 配置文件,您应该使用 CSV 文件或数据库表。如果您想将其保存在 CSV 文件中,那么我建议您为每种配置类型保留一个 CSV 文件(例如,用于数据库配置的 CSV 文件)并始终使用 file_put_contents 覆盖前一个文件
保存数据示例:
$csvStructure = array("dbUser","dbPassword","dbHostname","dbPort"); // array used for both loading data and saving it
$csvData = array();
foreach ($csvStructure as $field) {
$csvData[] = $_POST[$field]; // so it'd get $_POST["dbUser"],$_POST["dbPasword"], etc..
}
file_put_contents("filename",implode("\t",$csvData));
加载数据示例:
$csvStructure = array("dbUser","dbPassword","dbHostname","dbPort"); // array used for both loading data and saving it
$dbConfig = array();
$csvData = explode("\t",file_get_contents("filename"));
foreach ($csvStructure as $key => $field) { // $key would have the location of the requested field in our CSV data (0,1,2, etc..).
$dbConfig[$field] = $csvData[$key]; // populate $dbConfig["dbUser"],$dbConfig["dbPasword"], etc..
}
我相信使用 ini 文件是一个明智的选择,因为用户、密码、模式、路径等通常是手动修改的,所以使用var_export
不是因为手动修改它不是那么干净,可能会导致你的崩溃如果您在 PHP 语法中犯了错误,则应用程序。
但是解析大的 ini 文件可能会很昂贵,所以用var_export()
or缓存 ini 就可以了serlialize()
。我认为这是一个更好的选择,并且仅在缓存文件不存在时才读取 ini。
好吧,要写一个文件,fwrite() php 函数正是你想要的。来自其 PHP.NET 文档页面(参见下面的示例)。
现在,关于向该文件输出什么的问题 - 我假设该文件必须作为配置 .php 文件包含到项目的其余部分中。我想象你会做这样的事情 - 你正在根据提交的表单使用 PHP 代码动态创建字符串:
$strDatabaseConfig = "\$databaseConfig = array('" . $_POST['login'] . "," . $_POST['password'] . "');";
这是 fwrite 的片段:
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
这是一种方式:wp-admin/setup-config.php
来自 WordPress。
我更喜欢有一个包含一堆定义语句的文件。
这些是全局可用的常量(当然是不可变的),这是您进行配置设置所需要的。
常量提供更好的内存管理和读取效率,因为它们不需要变量所需的额外内存以便可以更改它。
假设您的 config.inc 文件如下所示:
$config = array(
'blah' => 'mmm',
'blah2' => 'www',
//...
);
你想更新它,所以你创建一个简单的表单,用当前值填充文本字段。覆盖当前配置的 PHP 脚本可能如下所示:
$newConfig = ...; // data from form - of course validate it first
$config = ...; // data from config.inc
$config = array_merge($config, $newConfig);
file_put_contents('config.inc', '<?php $config = ' . var_export($config, true));
你完成了。