【问题标题】:How to get file name in File Upload CodeIgniter如何在文件上传 CodeIgniter 中获取文件名
【发布时间】:2020-08-17 13:09:21
【问题描述】:

我正在尝试在 codeigniter 中做一个文件上传器,它将文件名保存在 phpmyadmin 数据库中。当我添加“照片”时,文件的实际名称没有保存。

控制器 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>

【问题讨论】:

  • phpmyadmin 不是数据库。

标签: php mysql database codeigniter file-upload


【解决方案1】:

将您的代码更改为

}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');
}

【讨论】:

  • 现在当我提交视图刷新时没有选择文件
猜你喜欢
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 2022-01-22
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多