我在codeigniter中的模型中有一个函数,它获取了数据库的模板,因此当我填写表单并单击我的提交按钮时,它将保存写入codeigniter中的database.php文件的输入。
我无法让两个@fopen工作。当我再添加一个位置时,清除我要写入的文件中的所有数据。
public function load_app_ins() {
$template = file_get_contents(APPPATH . 'modules/install/config/database.php');
$replace = array(
'HOSTNAME' => $this->input->post('hostname'),
'USERNAME' => $this->input->post('username'),
'PASSWORD' => $this->input->post('password'),
'DATABASE' => $this->input->post('database'),
'DBDRIVER' => $this->input->post('dbdriver'),
'DBPREFIX' => $this->input->post('dbprefix')
);
$template = str_replace(array_keys($replace), $replace, $template);
// Trying To Get Main Directory APPLICATION Not Load Clears Data Should Load
$file = @fopen(dirname(FCPATH) . '/application/config/database.php', 'w+');
// This Location Works OK From Install Directory
$file = @fopen(APPPATH . 'config/database.php', 'w+');
if ($file !== false) {
$response = @fwrite($file, $template);
fclose($file);
if ($response) {
return true;
} else {
echo 'Error when process'; die;
}
}
答案 0 :(得分:0)
我在codeigniter用户指南上阅读了更多内容,发现方式更容易。 write_file现在好多了。
unset($this->db);
$dsn = $this->input->post('db_driver').$this->input->post('db_username').':'. $this->input->post('db_password').'@'.$this->input->post('db_hostname').'/'.$this->input->post('db_database');
if (is_resource($this->db->conn_id) OR is_object($this->db->conn_id)) {
$data = array();
$data['db_hostname'] = $this->input->post('db_hostname');
$data['db_username'] = $this->input->post('db_username');
$data['db_password'] = $this->input->post('db_password');
$data['db_database'] = $this->input->post('db_database');
$data['db_driver'] = $this->input->post('db_driver');
$data['db_prefix'] = $this->input->post('db_prefix');
$get_template_file_contents = $this->load->view('template/configuration/database.php', $data, true);
write_file(APPPATH . 'config/database.php', $get_template_file_contents, 'r+');
write_file(dirname(FCPATH) . '/admin/application/config/database.php', $get_template_file_contents, 'r+');
write_file(dirname(FCPATH) . '/catalog/application/config/database.php', $get_template_file_contents, 'r+');
redirect('setup/step_4');
}