在codeigniter中在线编辑自定义配置文件

时间:2015-12-03 16:19:00

标签: php file codeigniter filepath

我创建了一个用于编辑自定义配置文件的模型 它的工作成功,除了文件目录

这是我的模特

class Settings_model extends CI_Model{

    // replace config items
    public function edit_line($word, $replace) {
      $base_url = $this->config->base_url();
        $reading = fopen('../application/config/config_custom.php', 'r');
        $writing = fopen('../application/config/myconf_tmp.php', 'w');

        $replaced = false;

        while (!feof($reading)) {
          $line = fgets($reading);
          if (stristr($line, $word)) {
            $line = "  '$word' => '$replace',\n";
            $replaced = true;
          }
          fputs($writing, $line);
        }
        fclose($reading); fclose($writing);
        // might as well not overwrite the file if we didn't replace anything
        if ($replaced)
        {
          rename($writing, $reading);
        } else {
          unlink($writing);
        }

    }

}

和我的控制器

class Settings extends My_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        if($this->input->post()) {
          foreach($this->input->post() as $key => $value){
              $edit = $this->Settings_model->edit_line($key, $value);
              echo $edit;
          }
        }

        // Load View
        $data['main_content'] = 'settings/index';
        $this->load->view('layouts/main',$data);
    }

}

并查看

<form action="<?php echo base_url().'settings/index';?>" method="post">
      <?php foreach($this->global_data as $key => $val):?>
      <div class="form-group">
          <label><?=$key?></label>
          <input type="text" name="config_item['<?=$key?>']'" class="form-control" value="<?=$val?>" />
      </div>
      <?php endforeach; ?>
      <button class="btn btn-success" type="submit">حفظ</button>
</form>

当我发布我的表单时,我收到此错误消息

A PHP Error was encountered

Severity: Warning

Message: fopen(../application/config/config_custom.php): failed to open stream: No such file or directory

Filename: models/settings_model.php

Line Number: 7

我应该在模型中为配置文件路径设置的正确路径是什么 请任何帮助!

1 个答案:

答案 0 :(得分:1)

对此进行了解读,

fopen('../application/config/config_custom.php', 'r');

尝试使用这样的,

fopen(APPPATH.'/config/config_custom.php', 'r');