Save Images from CodeIgniter

0

Hello, I'm looking to save an image to the database. I'm using the following:

  $config['upload_path'] = './assets/fotos/';
  $config['allowed_types'] = '*';
  $config['max_size']     = '512000';
  $config['max_width']  = '2440';
  $config['max_height']  = '1600';
  $this->load->library('upload', $config);
  $this->upload->initialize($config);
  $this->upload->do_upload('arquivo');
  $imagem = $this->upload->data();
  $file_url = base_url("assets/fotos/{$imagem['file_name']}");

My input is like this:

<div class="form-group col-sm-12">
<label class="col-sm-2">Imagem do Produto:</label>
  <input type="file" name="arquivo" id="arquivo" size="99999" class="col-sm-7">
</div>

To save the image, in addition to the insert method in the model I have in the insert method:

"foto" => $file_url

What happens, the photo path is saved (without the file in question of course), for example:

link

But you are not saving the photo to the informed folder. What am I forgetting? Thank you.

    
asked by anonymous 26.03.2017 / 18:41

1 answer

0

There are some possibilities like:

  • Maximum file size may be exceeding the size in MB allowed by php.ini;
  • (Primarily) if it is Linux you will have to add write permission to your user and group in the directory where you are trying to save the images;
  • The form must contain the enctype="multipart/form-data" attribute. Example: <form method="post" action="#" enctype="multipart/form-data">

When uploading check the error message on a conditional.

if (!$this->upload->do_upload('arquivo')) {
    // Se deu erro...
    echo $this->upload->display_errors();

} else {
   // Segue processamento...
}
    
09.05.2017 / 19:42