A
A
Alexey Gorbunov2020-05-01 11:52:08
PHP
Alexey Gorbunov, 2020-05-01 11:52:08

PHP Class: file_get(put)_contents. How to save form values ​​to different files?

Happy Labor Day colleagues! I spend May Day at home in the works :)

How to save form values ​​​​in different files and read them. Confused with the development of Class. But I can't figure out how to implement it.

The input has a Class that reads and saves form values ​​to a single file:

class Admin {

    // Пути к файлам
    protected $basePath;
    protected $configFile;
    protected $valuesFile;

    // Настройки с их значениями
    protected $settings = array();

    // Конструктор
    function __construct() {
        $this->basePath = $_SERVER['DOCUMENT_ROOT'] . '/admin/';
        $this->configFile = $this->basePath . 'config/config.json';
        $this->valuesFile = $this->basePath . 'config/values.json';

        $this->setSettings();
    }

    // Инициализируем настройки
    private function setSettings() {
        $config = json_decode(file_get_contents($this->configFile), true);
        $values = json_decode(file_get_contents($this->valuesFile), true);

        foreach($config as $item) {
            $value = isset($values[$item['key']]) ? $values[$item['key']] : $item['default'];
            if ($item['type'] == 'number') {
                $value = (int)$value;
            }
            if ($item['type'] == 'checkbox') {
                $value = $value === 'true';
            }

            array_push($this->settings, array(
                'utm' => $item['utm'],
                'key' => $item['key'],
                'title' => $item['title'],
                'type' => $item['type'],
                'list' => isset($item['list']) ? $item['list'] : null,
                'value' => $value
            ));
        }
    }

    // Возвращаем все настройки
    public function getSettings() {
        return $this->settings;
    }

    // Возвращаем значение одной настройки
    public function getValue($key) {
        $index = array_search($key, array_column($this->settings, 'key'));

        return $index !== false
            ? $this->settings[$index]['value']
            : null;
    }

  /* БЫЛО
    // Сохраняем настройки в файл
    public function save($settings) {

        $value = 'utm'; // вот здесь нужно вставить значение utm из поля формы 

        file_put_contents($this->basePath . 'config/'.$value.'.json', json_encode($settings));
        $this->setSettings();
    }  */

/* СТАЛО (РЕШЕНО) */

   // Сохраняем настройки в папку и новый файл 
    public function save($settings) {
        $utm = $_POST["utm"];
        file_put_contents($this->basePath . 'config/utm/'.$utm.'.json', json_encode($settings));
        $this->setSettings();
    }

}


How to get value $value = 'utm'; from a form field? Maybe ajax somehow?

But how to implement it competently with classes in php7?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Neverov, 2020-05-01
@AGorbunoff

1) Randomize the file name, or use a timestamp instead of the name, or both.
2) Get the same way you get other data from a form

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question