Как получить имя файла в File Upload CodeIgniter - PullRequest
0 голосов
/ 03 мая 2020

Я пытаюсь сделать загрузчик файлов в 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);
}

view 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>

1 Ответ

0 голосов
/ 03 мая 2020

Измените свой код на

}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');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...