Issue
Is there any special function to parse a .conf file in php like parse_ini_file()
function? If not how can I achieve that? Thanks!
EDIT :
Conf's like httpd.conf(Apache). (I want to read and edit httpd.conf file)
Solution
No, there is no special function to parse httpd.conf, but a parser should be easy to write. For example, if you're only interested in the key-value settings, like ServerRoot /var/www
, then this will do the trick:
<?php
define('HTTPD_CONF', '/tmp/httpd.conf');
$lines = file(HTTPD_CONF);
$config = array();
foreach ($lines as $l) {
preg_match("/^(?P<key>\w+)\s+(?P<value>.*)/", $l, $matches);
if (isset($matches['key'])) {
$config[$matches['key']] = $matches['value'];
}
}
var_dump($config);
If you want to parse the <Directory ...>
and other blocks, it'll take you a few more lines of code, but it shouldn't be too taxing. It really depends on what you want to do. You can get a lot of info from $_SERVER
and getenv()
, so you might not need to parse a config file.
EDIT
In response to your update, for editing httpd.conf, you'll need to run your script with superuser privileges and cause httpd.conf to be reloaded (e.g., system("apachectl graceful");
).
Answered By - jmdeldin