The method variable $ config for setting the configuration file in PHP is the configuration information we submitted and stored in arrays. PHP configuration file settings
The variable $ config is the configuration information we submitted and stored as an array.
function set_config($config){ if(!is_array($config)) return FALSE; $configfile = CONF_PATH.'config.php'; if(!is_writable($configfile)) return('Please chmod ./config.inc.php to 0777 !'); $pattern = $replacement = array(); $str = file_get_contents($configfile); foreach($config as $k=>$v){ $pattern[$k] = "/['\"]".strtoupper($k)."['\"]\s*=>\s*([']?)[^']*([']?)/is"; $replacement[$k] = "'".strtoupper($k)."'=> \${1}".$v."\${2}"; } $str = preg_replace($pattern, $replacement, $str); return file_put_contents($configfile, $str); }
The above $ configfile is the complete path of the file, that is, the absolute path.
Use the file_get_contents file to obtain all the information in the configuration file.
Use the preg_replace method to replace the submitted configuration information with the original configuration information.
Replace all modified information with file_put_contents.
The above method can be used not only to modify configuration information, but also to modify any file