我正在尝试使用html表单编辑配置文件。 edit(settings.php)文件如下所示:
$config['foo'] = FALSE;
$config['maintenance'] = FALSE; //this line is that what it matters
$config['bar'] = FALSE;
这里的想法是改变$config['maintenance']
,所以一旦表单被提交(有一个名为maintenance
的复选框,以便根据状态将状态设置为true或false),我获取复选框值:
$status = ($_POST['maintenance'] === 'on')? "TRUE" : "FALSE";
我调试了$status
var值,一切都很顺利。现在,我使用下面的正则表达式在文件中找到正确的行:
\$config\[(\s+)?(\'|")maintenance(\'|")(\s+)?\](\s+)?=(\s+)?(false|FALSE|true|TRUE);/
最初“工作”很好,因为我不确定,但让我完成解释...... 根据上面的代码,现在我继续做替换:
//read the content and replace it
$content = preg_replace(
'/\$config\[(\s+)?(\'|")maintenance(\'|")(\s+)?\](\s+)?=(\s+)?(false|FALSE|true|TRUE);/',
'$config["maintenance"] = ' . $status . ';',
file_get_contents($file)
);
//set the new content
file_put_contents($file, $content);
当我第一次运行它时,选中复选框就可以了,结果如下:
$config['foo'] = FALSE;
$config["maintenance"] = TRUE;
$config['bar'] = FALSE;
但是,无论我在复选框中选择什么,该文件都不会显示任何更改。你能引导我找到正确的方向找到这个bug吗?谢谢
编辑。 这是html标记
<label>
<input type="checkbox" name="maintenance" /> in maintenance mode
</label>
答案 0 :(得分:1)
试试这个:
$status = (isset($_POST['maintenance'])) ? 'TRUE' : 'FALSE';
和
$content = preg_replace(
'/\$config\[\s*[\'"]maintenance[\'"]\s*\]\s*=\s*(false|true);/i',
'$config["maintenance"] = ' . $status . ';',
file_get_contents($file)
);
答案 1 :(得分:1)
但是你发布的代码对我来说很好,你应该做更多的调试,如:
error_reporting(-1);
或在更换前后检查$ content。检查错误日志(如果将display_errors设置为on,则搜索错误消息)。可能有任何错误。 (例如文件权限)。
还要考虑: