Shehroz Asmat
Shehroz Asmat

Reputation: 161

Upload File from Rest client to codeigniter

I know it could be a duplicate but i need a bit more help in that. I have started working on making api for an android application in codeigniter. And currently working on making api for uploading an image.. I am using https://github.com/chriskacerguis/codeigniter-restserver using postman to upload images to test my API but but i am getting something like

'------WebKitFormBoundaryXkKFa0QV2XVs0EoK Content-Disposition:_form-data;_name' => string '"filesss"; filename="17264394_1739947152983817_1705887364349350305_n.jpg" Content-Type: image/jpeg �����JFIF����������Photoshop 3.0�8BIM������g�Vnlfejt43YHm4REx3e4k(�bFBMD01000abe0300002f19000082320000643300004b3400002f4d0000c46f0000d3750000b8780000b07b000096cd0000��ICC_PROFILE��� lcms��mntrRGB XYZ

Now can any one help me how to be able to save the image on my server below is my code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . './libraries/REST_Controller.php';
use Restserver\Libraries\REST_Controller;
class Update_image extends REST_Controller{
    function __construct(){
        parent::__construct();
            $models[] = 'Quiz_model';
            $models[] = 'Users_model';
        $this->load->model($models);    
            $libs[] =  'form_validation';
            $libs[] =  'image_lib';
            $libs[] =  'email';
            //$libs[] =  'rest';
        $this->load->library($libs);

            $helpers[] = 'file';
            $helpers[] = 'security';
            $helpers[] = 'url';
            $helpers[] = 'string';
        $this->load->helper($helpers);


    }

    function index_post(){


            $users_id = $this->input->post('users_id');
            $users_id = 6;

            $config['upload_path']   = './public/user_images/';
            $config['allowed_types'] = 'jpg|png';
            $config['max_size']      = ; // for 5mb file
            $config['max_width']     = 5000;
            $config['max_height']    = 5000;
            $this->load->library('upload', $config);

            if(!$this->upload->do_upload('users_image')){
                $error = array('error' => $this->upload->display_errors('<span>','<span>'));
                $data['msg'] = error('Please Fix the errors below');
                $data['file_upload_error'] = formErrorParse($this->upload->display_errors());
                $this->form_validation->set_message('quiz_image', implode('<br/>', $error));

                $response['status'] = 0;
                $response['msg'] = strip_tags($this->upload->display_errors());

            }else{

                $uploadStat = $this->upload->data();
                $img_config['image_library'] = 'gd2';
                $img_config['source_image'] = './public/uploads/user_images/'.$uploadStat['file_name'];
                $img_config['new_image'] = './public/uploads/user_images/thumbnail/'.$uploadStat['file_name'];
                //$img_config['create_thumb'] = TRUE;
                $img_config['maintain_ratio'] = TRUE;
                $img_config['width']     = 150;
                $img_config['height']   = 150; 
                $this->image_lib->clear();
                $this->image_lib->initialize($img_config);
                $this->image_lib->resize();

                $updates['users_images'] = $uploadStat['file_name'];
                $cond['users_id'] = $users_id;
                $this->db->update('users', $updates, $cond);

                $response['status'] = 1;
                $response['msg'] = 'Image Uploaded';
            }
            $this->response($response, 200);
    }   

}

Upvotes: 0

Views: 2619

Answers (2)

Jude Fernandes
Jude Fernandes

Reputation: 7527

Make sure your php version is 5.6 or above,i had the same problem and solved it by switching to php version 7.

Upvotes: 0

Amit Ahire
Amit Ahire

Reputation: 61

I had exactly same problem as you. But solved after removing Content-Type from headers in postman. Then you'll get data in $_FILES.

Upvotes: 1

Related Questions