在编辑PHP时显示文件的现有内容

时间:2016-03-03 06:57:47

标签: php codeigniter

我打开.ini,然后写一些为其参数添加一些值,然后我将其保存回来。

我写的内容完全正常,但是当我打开文件时,该文件的先前内容被清除。[可能是由于我的写入模式]。

我应该在代码中做些什么更改,让用户在编辑文件时看到以前的内容。

file_get_contents($path.$filename); 

    /*Get All The datas from that file*/

    $this->data['params'] = $this->parameter_m->get();

    /*Getting the parameters to display on view*/

    $this->data['parameters']  = parse_ini_file($path.$filename,true);

    while (current($this->data['parameters']) ) 
    {
        $param_set = current($this->data['parameters']);
        $param_type = key($this->data['parameters']);

            foreach ($param_set as $key =>$value) 
            {

                $this->data['parameters'][$param_type][$key] = $this->input->post($key);

            } 

        next($this->data['parameters']);
    }

    $this->load->helper('file');

    $this->load->library('ini');

    $file = $path.$filename;

    $ini = new INI($file);

    $ini->read($file);



    $ini->write($file, $this->data['parameters']);

我的写功能

function write($file = NULL, $data_file = array(), $sections = TRUE) {
    $this->data_file = (!empty($data_file)) ? $data_file : $this->data_file;
    $this->file = ($file) ? $file : $this->file;
    $this->sections = $sections;
    $content = NULL;

    if ($this->sections) {
        foreach ($this->data_file as $section => $data_file) {
            $content .= '[' . $section . ']' . PHP_EOL;
            foreach ($data_file as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $v) {
                        $content .= $key . '[] = ' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
                    }
                } elseif (empty($val)) {
                    $content .= $key . ' = ' . PHP_EOL;
                } else {
                    $content .= $key . ' = ' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
                }
            }
            $content .= PHP_EOL;
        }
    } else {
        foreach ($this->data_file as $key => $val) {
            if (is_array($val)) {
                foreach ($val as $v) {
                    $content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
                }
            } elseif (empty($val)) {
                $content .= $key . ' = ' . PHP_EOL;
            } else {
                $content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
            }
        }
    }

    return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}

0 个答案:

没有答案