I’m trying to edit a config file using a html form. The edit (settings.php) file looks like this:
$config['foo'] = FALSE;
$config['maintenance'] = FALSE; //this line is that what it matters
$config['bar'] = FALSE;
The idea here is change the of $config['maintenance'], so once the form is submitted (there is a checkbox named maintenance in order to set the status to true or false according to its state), I get the checkbox value as:
$status = ($_POST['maintenance'] === 'on')? "TRUE" : "FALSE";
I have debugged $status var value and everything goes fine to here. Now, I am using the regex below to find the correct line at file:
\$config\[(\s+)?(\'|")maintenance(\'|")(\s+)?\](\s+)?=(\s+)?(false|FALSE|true|TRUE);/
Initially “works” good, because I am not sure, but let me finish the explanation…
According with the code above, now I proceed to do the replacement:
//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);
When I run it the first time with the checkbox checked it works and the result is as follow:
$config['foo'] = FALSE;
$config["maintenance"] = TRUE;
$config['bar'] = FALSE;
However, no matter what I select in the checkbox, the file does not show any changes. Can you guide me to the right direction to find the bug? Thank you
Edit.
This is the html markup
<label>
<input type="checkbox" name="maintenance" /> in maintenance mode
</label>
Try this:
and: