我的一个站点有一个 config.php 文件,其中包含运行该站点所需的信息。它看起来像这样:
<?php
$site_name="The Site";
$base_url = "http://site.com";
$upload_path = "/images";
?>
从中获取值很容易,我只使用 require'config.php';
但我如何提供从 html 表单添加它的选项?意味着我如何更新值?我想到的一种方法是使用 fopen 和 fsave,有点像这样:
<?php
$filename = 'config.php';
$somecontent = "config here";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
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";
}
?>
但这是执行此操作的标准方法吗?这就是所有这些 cms 编写配置文件的方式吗?