Save Images from Codeigniter

Asked

Viewed 754 times

0

Hello, I am wanting to save an image in the database. I am 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']}");

Already 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, is saved the path of the photo (without the file in question of course), for example:

http://[::1]/Nameapp/Assets/photos/

But you’re not saving the photo in the briefcase. What am I forgetting? Thank you.

  • Are you sure the directory in question is writable?

1 answer

0


There are some possibilities like:

  • The maximum file size may be exceeding the MB size allowed by php.ini;
  • (Mainly) 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 shall contain the attribute enctype="multipart/form-data". 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...
}

Browser other questions tagged

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