does not accept images codeigniter

0

Why does not the image accept me?

Code

public function imagedata_post(){
    if (!isset($_FILES['userfile']['name'])) {
        $this->response(array('message'=>'no image'),200);
    } else {
            $date = date("Y-m-d");
            $time = date("H:i:s", strtotime('+12 hours'));
            $ram = $id."_".$date."_".$time."_".$type;

            $file_name = $_FILES['userfile']['name'];
            $tmp = explode('.', $file_name);
            $extension_img = end($tmp);
            $image = $ram . '.' . $extension_img;


            $config['upload_path'] = './assets/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '5000000';
            $config['quality'] = '20%';
            $config['file_name'] = $image;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload()) {
               $this->response(array('message'=>'ok'),200);
            }else{
                $this->response(array('message'=>'no upload file'),200);
            }
        }
}

postman

    
asked by DoubleM 05.06.2018 в 19:31
source

1 answer

1

You need to initialize the upload setting using $this->upload->initialize($config); and it also depends on how you are sending the image.

Example:

if (!empty($_FILES['img_1']['name'])){
        $config['upload_path'] = 'assets/img/images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if ($this->upload->do_upload('img_1')){
            $img_1 = $this->upload->data();
        }else{
            echo $this->upload->display_errors();
        }
    }
    
answered by 05.06.2018 в 21:43