There is a configuration file config.php. Settings are stored in an associative array. It is necessary that you can overwrite the key values ​​in the array from the admin panel. That is, that they are overwritten in the file. I already wrote the form and handler. The data is all coming. I can display on the screen, but I don’t write to the file. Through fwrite () stupidly erases everything and writes one line.
Here is the config.php:

$config = array( 'description' => 'Введите описание!', 'keywords' => 'ключевые слова', 'blog_name' => 'Название сайта', 'template' => 'briton', 'db' => array( 'host' => 'localhost', 'user' => 'root', 'password' => '', 'basename' => 'armor' ) ); 

1 answer 1

More on var_export .

 $config = array( 'description' => 'Введите описание!', 'keywords' => 'ключевые слова', 'blog_name' => 'Название сайта', 'template' => 'briton', 'db' => array( 'host' => 'localhost', 'user' => 'root', 'password' => '', 'basename' => 'armor' ) ); $contents = var_export($config, true); // Если передано и значение равно TRUE, var_export() вернет представление переменной вместо его вывода. file_put_contents('config.php', "<?php\n return {$contents};\n"); 

Result in config.php :

 <?php return array ( 'description' => 'Введите описание!', 'keywords' => 'ключевые слова', 'blog_name' => 'Название сайта', 'template' => 'briton', 'db' => array ( 'host' => 'localhost', 'user' => 'root', 'password' => '', 'basename' => 'armor', ), ); 


UPDATE To simply overwrite the string.

 $for_edit = "template"; // Ищем ключ который нужно заменить $what = "'template' => 'my_own',\n"; // А тут делаем строку из ключа и нового значения этого ключа $fopen = @file("config.php"); foreach ($fopen as $key => $value) { if (substr_count($value,$for_edit)) { array_splice($fopen, $key, 1, $what); // Если нашли совпадение, то заменяем } $f = fopen("config.php", "w"); // Перезаписали в файл for($i = 0; $i < count($fopen); $i++) { fwrite($f, $fopen[$i]); } fclose($f); } 
  • I can output the array, but how can I overwrite one of the values ​​in the file itself? Let's say we get the value of "Site name 2" from _POST and change the site name to Site name 2 so that it is copied in the file itself - Ivan
  • @ Ivan, Updated. - entithat
  • Thank you from the heart, Dear! Everything works great! I will go now to think how to change several parameters. Contacting does not save, it is necessary to smoke manuals. - Ivan
  • @ Ivan, use your health :)) - entithat