簡體   English   中英

如何在文件上傳 CodeIgniter 中獲取文件名

[英]How to get file name in File Upload CodeIgniter

我正在嘗試在 codeigniter 中做一個文件上傳器,它將文件名保存在 phpmyadmin 數據庫中。 當我添加“照片”時,不會保存文件的實際名稱。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM