如何在Codeigniter中从rest客户端上传图片并将文件名保存到数据库


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

我想使用codeigner将图片从rest客户端上传到服务器,同时将文件名保存到数据库。但它只保存其他数据,图片和文件名根本没有上传

这是api模型。

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控制器代码

    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) ;
}

请帮忙,谢谢

您的do_updoad_data()错误,请参阅下面的更改必须设置父变量,即$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;
  }
}