Error!!! You did not select the file to upload. Codeigniter

Asked

Viewed 862 times

3

I have a problem uploading the image and registering it in my database.

In my database I have a table called tbl_article where there is a when going to the image in question called artigo_image with a varchar(80):

The view is like this:

<div class="form-group">
    <label class="control-label col-md-1 col-sm-1 col-xs-12">Imagem do artigo</label>
    <div class="col-md-11 col-sm-11 col-xs-12">
    <!-- Vou verificar se existe imagem cadastrada -->

        <?php
        if (property_exists($artigo, 'aritigo_imagem') && $artigo->artigo_imagem != "") {

            echo "<img width='200px' height='200px' src='" . base_url('tema/assets/img/artigo/' . $artigo->artigo_imagem) . "'  title='" . $artigo->artigo_titulo . "' class='img-responsive img-rounded'> ";
            echo '<div id="remover"> <a href="' . base_url("admin/artigo/rimagem/" . $this->uri->segment(4)) . '"> Remover Imagem </a> </div>';
        } else {

            echo '<div class="fileinput fileinput-new" data-provides="fileinput">';
            echo '<div class="fileinput-new thumbnail" data-trigger="fileinput">';
            echo '<img src="' . base_url('tema/assets/img/acao/200x200.png') . '" class="user-image" alt="...">';
            echo '</div>';
            echo '<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 200px"></div>';
            echo '<div>';
            echo '<span class="btn btn-primary btn-file">';
            echo '<span class="fileinput-new"> <i class="fa fa-file-image-o"></i> Mudar a Imagem </span>';
            echo '<span class="fileinput-exists"> <i class="fa fa-file-image-o"></i> Carregar </span>';
            echo '<input type="file" name="imagem_link" accept="image/*">';
            echo '</span>';
            echo '<a href="#" class="btn btn-danger fileinput-exists" data-dismiss="fileinput"> <i class="fa fa-close"></i> Apagar </a>';
            echo '</div>';
            echo '</div>';
        }
        ?>
    </div>
</div>

My controller is like this:

public function cadastro() {

    $validacao = self::validar('cadastro');

    if (($validacao == '1') && (!empty($_POST) == '1')) {
        // Configuraçao da Imagem
        $config['upload_path'] = './tema/assets/img/artigo';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0';
        $config['max_width'] = '0';
        $config['max_height'] = '0';
        $config['encrypt_name'] = true;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('artigo_imagem')) { // Se o Upload de errado
            $this->session->set_flashdata('error', $this->upload->display_errors());
        } else { // Se o Upload de Sucesso
            // Salvamos os dados do form em um array.
            $_data = $this->input->post();
            $_data['artigo_imagem'] = $this->upload->do_upload('file_name');
            // Definimos como padrao o artigo habilitado...
            $_data['artigo_status'] = 0;

            // Pegamos o retorno do cadastro em uma variavel.
            $status = $this->artigo->Inserir($_data);  // Aqui inserir os dados no bd...
            // Se retornar 1 no Status e pq o cadastro foi inserido no bd.

            if ($status) {
                $this->session->set_userdata('success', 'Artigo Cadastrado com Sucesso.');
                redirect('admin/artigo'); // Redirecionamos para o modulo artigo..
            } else { // Se o cadastro retornar 0 e pq deu algum erro e nao redirecionara.
                $this->session->set_userdata('error', 'Erro ao Cadastrar o Artigo.');
            }
        }
    } else { // Aqui e se nao passar pela validaçao do formulario...
        $this->session->set_userdata('error', validation_errors('<p>', '</p>'));
    }

    $dados = array(
        'artigo' => new stdClass(), // retorna um array vazio sem erros
        'titulo' => 'Novo Artigo',
        'titulo_imagem' => $this->artigo->GetAll(false, "artigo_imagem", 'asc'),
        'botao' => 'Cadastrar'
    );

    $this->template->load('admin/tema_admin', 'admin/artigo/artigo_form', $dados); // Template, Views e Dados
}

when I go to upload the image says according to the validator that I have not selected the image that theoretically is not true.

I have other functions like this one, and yet this one is giving this error what can be?

  • <input type="file" name="imagem_link", here you would have to put the id="imagem_link"

  • It may be the permission in the folder, you need to catch the specific error with $this->upload->display_errors()

  • I gave the permissions and still nothing . There’s changed name="" to id="" and nothing

1 answer

3


In your HTML you are

<input type="file" name="imagem_link" accept="image/*">

In your controller you are using

!$this->upload->do_upload('artigo_imagem'),

The input name must be equal to the name passed in the function argument do_upload. Either rename the input name to "article_image" or change "article_image" to "imagem_link".

It should be so then:

!$this->upload->do_upload('imagem_link')

Also your form must have the following attribute enctype. Here is usually the likely error with uploads. I’ve forgotten that a lot.

<form action="upload.php" method="post" enctype="multipart/form-data">

Bonus

No need to write HTML inside the command echo. It gets really bad to read and give maintenance. Try it like this:

<?php if (property_exists($artigo, 'aritigo_imagem') && $artigo->artigo_imagem != "") { ?>

 <img width='200px' height='200px' src='" . base_url('tema/assets/img/artigo/' . $artigo->artigo_imagem) . "'  title='" . $artigo->artigo_titulo . "' class='img-responsive img-rounded'> 
 <div id="remover"> <a href="' . base_url("admin/artigo/rimagem/" . $this->uri->segment(4)) . '"> Remover Imagem </a> </div>
<?php } else { ?>

  <div class="fileinput fileinput-new" data-provides="fileinput">
        <div class="fileinput-new thumbnail" data-trigger="fileinput">
             <img src="' . base_url('tema/assets/img/acao/200x200.png') . '" class="user-image" alt="...">
        </div>
        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 200px"></div>
             <div>
                 <span class="btn btn-primary btn-file">
                   <span class="fileinput-new"> <i class="fa fa-file-image-o"></i> Mudar a Imagem </span>
                   <span class="fileinput-exists"> <i class="fa fa-file-image-o"></i> Carregar </span>
                   <input type="file" name="imagem_link" accept="image/*">
                 </span>
                 <a href="#" class="btn btn-danger fileinput-exists" data-dismiss="fileinput"> <i class="fa fa-close"></i> Apagar </a>
            </div>
      </div>                            
<?php } ?>
  • Justly man enctype="Multipart/form-data">

  • If that was it and solved, do not forget to accept the answer! Needing we are there

  • Please note that the answer has resolved the problem (not a positive vote) http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply

  • I am now starting to mess with this tool . I forgot now I think it worked. Very obg

Browser other questions tagged

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