简体   繁体   中英

How to upload a picture from rest client in Codeigniter and save file name to database?

I want to upload picture from rest client to server and save file name at the same time to database using codeigniter. but it only save the other data, the picture and filename didnt upload at all

here is api model.

function save_confirm()
{
    $config['upload_path']   = './upload/';   
    $config['allowed_types'] = 'jpg|png';
    $config['overwrite']     =  TRUE;    
    $config['max_size']      = '100000';
    $config['max_width']     = '100000';
    $config['max_height']    = '100000';

    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    $this->upload->do_upload("pict");
    $data = array('upload_data'=>$this->upload->data("pict"));      
    $file_name = $data['upload_data']['file_name'];  

  $data = array(
    'id_booking'      => $this->input->get('id_booking'),
    'username'        => $this->get_username_by_user_token(),
    'transfer_date'   => date('Y-m-d'),
     'pict'           => $file_name
    );
  $this->db->insert('confirmation',$data);
  return $this->db->insert_id();
}

api controller code

    public function confirm_get(){
    $get_confirm = $this->api_model->save_confirm();

    $data = array(
        'meta' => array(
            'status'  => 200,
            'message' => "OK",
            "code"    => 9
        ));
    $this->response($data,200) ;
}

please help, thank you

You have your do_updoad_data() wrong see changes below must set parent variable ie $file_data = $this->do_upload->data();

function save_confirm()
{
    $config['upload_path']   = './upload/';   
    $config['allowed_types'] = 'jpg|png';
    $config['overwrite']    =  TRUE;    
    $config['max_size']      = '100000';
    $config['max_width']     = '100000';
    $config['max_height']    = '100000';

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


    if ($this->upload->do_upload("pict") == true) {

    $file_data = $this->do_upload->data();
    $file_name = $file_data['file_name'];

    // http://www.codeigniter.com/user_guide/libraries/file_uploading.html

    // Examples
    $file_type = $file_data['file_type'];
    $file_path = $file_data['file_path'];
    $full_path = $file_data['full_path']; 

    $data = array(
    'id_booking'       => $this->input->get('id_booking'),
    'username' => $this->get_username_by_user_token(),
    'transfer_date'   => date('Y-m-d'),
     'pict'     => $file_name
    );

    $this->db->insert('confirmation',$data);
    return $this->db->insert_id();

  } else {

    // return false;

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM