如何在file Upload CodeIgniter中获取文件名



我正在尝试在codeigniter中进行文件上传,将文件名保存在phpmyadmin数据库中。当我添加"foto"时,文件的实际名称不会保存。

控制器ctr_fotos.php:

public function add2()
{
$data['id_evento'] = $this->input->post('id_evento');
//file upload code 
//set file upload settings 
$config['upload_path']          = APPPATH. '../images/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 100;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('nova_foto', $error);
}else{
//file is uploaded successfully
//now get the file uploaded data 
$upload_data = $this->upload->data();
//get the uploaded file name
$data['foto'] = $upload_data['file_name'];
//store pic data to the db
}
$this->load->model('fotos_model');
$this->fotos_model->RegistarFoto($data);
redirect('Ctr_fotos/all');
}

型号fotos_Model.php:

public function RegistarFoto($data)
{
$insert_data['id_evento'] = $data['id_evento'];
$insert_data['foto'] = $data['foto'];
$this->db->insert('fotos', $insert_data);
}

查看nova_foto.php:

<body>
<div class="jumbotron">
<h1>Criar Foto</h1>
</div> 
<div class="container">
<?php
echo form_open("Ctr_fotos/add2");
?>
<div class="form-group">
<label for="nome" class="control-label">Id do Evento</label>
<input type="text" class="form-control" name="id_evento" value="<?php echo set_value('id_evento');?>">
</div>
<div class="form-group">
<label for="texto" class="control-label">Texto</label>
<input type="file" class="form-control" name="foto" id="foto">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="bt_submeter" value="Adicionar">
</div>

将代码更改为

}else{
//file is uploaded successfully
//now get the file uploaded data 
$upload_data = $this->upload->data();
//get the uploaded file name
$data['foto'] = $upload_data['file_name'];
//store pic data to the db
$this->load->model('fotos_model'); 
$this->fotos_model->RegistarFoto($data);
redirect('Ctr_fotos/all');
}

相关内容

  • 没有找到相关文章

最新更新