Home > Article > Backend Development > How to Modify a Configuration File String Using PHP Form Data?
Question:
How can one modify a string in a configuration file using data from a PHP form ($_POST variable)?
Answer:
Consider using a structured file format like CSV, Ini, XML, JSON, or YAML. Utilize dedicated APIs to read and write these formats.
Alternative Approaches:
Example:
A basic PHP class for managing configuration files:
class MyConfig { public static function read($filename) { $config = include $filename; return $config; } public static function write($filename, array $config) { $config = var_export($config, true); file_put_contents($filename, "<?php return $config ;"); } }
Usage:
// Write to config file MyConfig::write('conf1.txt', ['setting_1' => 'foo']); // Read and modify config in-memory $config = MyConfig::read('conf1.txt'); $config['setting_1'] = 'bar'; $config['setting_2'] = 'baz'; // Update config file with modified values MyConfig::write('conf1.txt', $config);
The above is the detailed content of How to Modify a Configuration File String Using PHP Form Data?. For more information, please follow other related articles on the PHP Chinese website!