Upload file with post

5

My view has this field:

<form class="form-horizontal"method="post" action="<?=base_url('index.php/home/cadastro')?>" enctype="multipart/form-data">
<input id="img" name="img" class="input-file" type="file">

In my controller I have the following:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {

public function index()
{
$this->load->view('v_home');
}

public function cadastro()
{
$titulo = $this->input->post('titulo');
        $texto = $this->input->post('texto');
        $link = $this->input->post('link');
        $nome_link = $this->input->post('nome_link');
        $img = $this->input->post('img');


        $dados =  array(
            'titulo' => $titulo,
            'texto' =>  $texto,
            'link' => $link,
            'nome_link' => $nome_link,
            'img' => $img,          
            'data' => date("Y-m-d H:i:s")
            );

//print_r($dados);
        $this->load->model('noticia_model');
$casdatro = $this->noticia_model->cadastrar_noticia($dados);

        if($casdatro){
            $this->session->set_flashdata('mensagem', 'Cadastro realizado com sucesso');
        }else{
            $this->session->set_flashdata('mensagem', 'Erro no cadastro');
        }

        //redirect(base_url('cadastrar_view'));


    }


public function cadastrar()
{
$this->load->view('cadastrar_view');
}


}

Only $img = $this->input->post('img'); is not picking up the ime

Does anyone help?

    
asked by anonymous 11.10.2016 / 20:20

1 answer

2

Fields of type file are not caught by $_POST (pure php) or input->post() (of codeginiter) but by the lib capenga that comes with the framework or $_FILES

//configuração do formato, tipo e tamanho do upload
$config['upload_path']          = './uploads/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 100;
$config['max_width']            = 1024;
$config['max_height']           = 768;

//Carrega a lib com a configuração passada em $config
$this->load->library('upload', $config);

//verifica se ocorreu um erro.
if (!$this->upload->do_upload('img')){
  echo $this->upload->display_errors());
}
    
11.10.2016 / 20:28