-1

I have this function:

function update_config($config)
        { 
         $buffer         = array();
         $buffer[]       = '<?php';
         foreach( $config as $key => $value ) {
                $buffer[]   = '$config[\'' .$key. '\'] = \'' .str_replace('\'', '&#039;', $value). '\';';
         }
         $buffer[]       = '?>';
            
            $data           = implode("\n", $buffer);
            $path           = $_SERVER['DOCUMENT_ROOT'] . 'settings.php';
        
            $fp = fopen($path, 'wb');
            if ($fp) {
                flock($fp, LOCK_EX);
                $len = strlen($data);
                fwrite($fp, $data, $len);
                flock($fp, LOCK_UN);
                fclose($fp);
            }
         }

is working very well, it insert like this $config[title] = 'Demo title';

How I can make to don't overwrite all file when I change something? Exemple, if I have 3 entries in settings.php and when I want to insert another, file is totaly rewrited with new insert only!

And I want when some exist like $config[title] change only value!

Thank you!

9
  • I'd use some structured data format for saving config - such as XML, JSON, YAML etc. Then You'll be able to load the complete data structure from file, make necessary changes and save it complete again. Commented Jan 30, 2022 at 19:38
  • I can't save complet, will be updated from many places! Commented Jan 30, 2022 at 19:40
  • 1
    Use a database for gods sake.. Commented Jan 30, 2022 at 19:41
  • I don't want to make request to database every time when I need info Commented Jan 30, 2022 at 19:42
  • 1
    Why? You think file I/O is faster? Commented Jan 30, 2022 at 19:47

2 Answers 2

2

With a little extra additions, var_export() will help you do what you're looking to. It accepts any PHP variable and will export it to a parsable representation.

Generally you would want to load the full config array, modify only desired values, and re-write the full array back to file.

You can do so like this:

$config = [
    "myValue1" => 10,
    "myValue2" => "Hello, world"
];

$configCode = '<?php $config = ' . var_export($config, true) . ';';

file_put_contents("config.inc.php", $configCode);
Sign up to request clarification or add additional context in comments.

Comments

0

better way is to save your config as a json file. then you can read all content into an array, change some variables and write the new one to the file.

{
  "title": "hello",
  "name": "test",
  "bla": "muh"
}
<?php
$string = file_get_contents('config.json');
$config = json_decode($string, TRUE);
$config['title'] = "new title";
$string = json_encode($config);
file_put_contents('config.json', $string);
?>

Edit:

same can you do with your $config array in php language:

include 'config.php'; // load all $config vars
$config['title'] = "new title";
// now write $config vars to file

4 Comments

While I agree, I'm down-voting because suggesting JSON isn't really within the scope of the question.
thanks, i added the php variable version too
As appreciated as that is, it doesn't actually answer the question.
Of course, but we can also refer to better implementation options here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.