error when uploading an image from a codeigniter form

0

Hi, I want to upload an image from a form in codeigniter but I have 2 problems:

1: I do not know how to put the extension of the original file.

2: I get this error: The upload path does not appear to be valid .

$nombreArchivo = "nombre_archivo";

$config['upload_path'] = base_url('/public/imagenes');
        $config['file_name'] = $nombreArchivo . "extension del archivo original";
        $config['allowed_types'] = "jpg|jpeg|png";
        $config['max_size'] = "50000";
        $config['max_width'] = "2000";
        $config['max_height'] = "2000";

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('imagen')) {
            //*** ocurrio un error
            $data['uploadError'] = $this->upload->display_errors();
            echo $this->upload->display_errors();
            die('error');
        }else {

            $nombreImagen = $data['file_name']; 

            $consulta=$this->db->query("INSERT INTO 'productos'('nombre', 'imagen') VALUES ('$nombre','$nombreImagen')");
    
asked by Doe 09.08.2017 в 11:56
source

1 answer

0

To find the extension use:

$nombre = $_FILES["imagen"]["name"]; 
$extension = end((explode(".", $nombre)));

Note that your input file must have as an attribute name="imagen"

<input type="file" name="imagen"/>

The $config['upload_path'] can not be an url, as you do in your case.

It has to be a route:

$config['upload_path'] = './imagenes/'

This would point you to a folder images that is on the same level as the index.php, that is, you would have something like this on the server:

  • application
  • system
  • images
    • Your uploads
  • index.php
answered by 09.08.2017 / 12:53
source