Upload file with post

Asked

Viewed 57 times

5

In my view there’s 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 that $img = $this->input->post('img'); is not getting the edge

Someone helps?

  • tries using $_FILE['img']

  • 3

    Possible duplicate of Upload images to Codeigniter

  • 1

    NO CI, you will not be able to upload this way, neither with file, nor with post, you will only be able to do it using his upload library, today they have also posted a question about it, see if it helps you: http://answall.com/questions/157944/erro-voc%C3%AA-n%C3%A3o-selected-o-file-to-do-upload-codeigniter

  • 1

    https://www.codeigniter.com/userguide3/libraries/file_uploading.html

1 answer

2


Fields of the type file are not caught by $_POST (pure php) or input->post() (codeginiter) but by the capenga lib that comes with the framework or the $_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());
}
  • thank you very much. I will test here.

  • I tried to but it is bringing me the following information:

  • Not Found The requested URL /admnoticias/uploadfile/upload was not found on this server. Apache/2.2.9 (Fedora) Server at www.cairu.br Port 80

  • @web_charles your upload folder is in the project root? the path to be passed should be relative to the project root.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.