在API Postman中的Codeigniter中使用db ID验证进行多图像上传

时间:2019-06-27 12:10:40

标签: php codeigniter image-uploading

我想基于 category_id vendor_id 在数据库中上传多个图像,包括使用Code-igniter进行验证 这是屏幕截图

Screenshot postman attchment

function upload_category_doc(){

        $vendor_id = $this->input->post("vendor_id");
        $category_id = $this->input->post("category_id");
        $response = array();
                if(isset($_FILES['category_doc']['name']) && $_FILES['category_doc']['name'] !=''){
                    $insertArr['category_doc']=$this->cat_upload('category_doc');
                }
                 $result = $this->providerapp_model->update_data("vbs_vendor_categories",$insertArr,array('vendor_id'=>$vendor_id,'category_id'=>$category_id));

                $fields = "category_id,vendor_id,category_doc";
                $sql1 = "SELECT $fields FROM vbs_vendor_categories vvc  WHERE vvc.vendor_id=$vendor_id AND vvc.category_id=$category_id";
                $vendor_cat_data = $this->db->query($sql1)->row();
                if($result){
                    $response['data'] = $vendor_cat_data;
                    $response['status'] = "success";
                    $response['message'] = "Category document update successfully";    
                }else{
                    $response['status'] = "fail";
                    $response['message'] = "Something went wrong";    
                }          
    }

及其上传功能以上传类别文档

  function cat_upload($imagename){     
    $date = date('His');

    $config['upload_path'] = 'uploads/category_doc';
    $config['file_name'] = 'category_doc' . $date;
    $config['allowed_types'] = 'jpg|png|bmp|pdf|doc|svg';

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

    if (!$this->upload->do_upload($imagename)) {

        $upload_error = array('error' => $this->upload->display_errors());
    } else {
        $upload_data = $this->upload->data();
        $config['image_library'] = 'gd2';
        $config['source_image'] = $this->upload->upload_path . $this->upload->file_name;
        $config['new_image'] = $this->upload->upload_path . "/thumb/" . $this->upload->file_name;
        $config['master_dim'] = 'auto';
        $config['width'] = 200;
        $config['height'] = 200;

        $this->load->library('image_lib');
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        return $upload_data['file_name'];
    }

}

2 个答案:

答案 0 :(得分:2)

$setting = $this->image_settings();
$this->load->library('upload', $setting);

$total = count(@$_FILES['image']['name']);

for($i=0; $i<$total; $i++)
{
    $_FILES['userfile']['name']= @$_FILES['image']['name'][$i];
    $_FILES['userfile']['type']= @$_FILES['image']['type'][$i];
    $_FILES['userfile']['tmp_name']= @$_FILES['image']['tmp_name'][$i];
    $_FILES['userfile']['error']= @$_FILES['image']['error'][$i];
    $_FILES['userfile']['size']= @$_FILES['image']['size'][$i];

    $this->upload->initialize($setting);

    $this->upload->do_upload();
    $actual_image_data = $this->upload->data();

    if(!empty($actual_image_data['is_image'])) {
        @$final_files_data[] = $actual_image_data['file_name'];
        $img_data['image_path'] = @$final_files_data[$i];
        $this->image_model->insert_image($img_data);
    } else {
        $response = [
            'status' => FALSE,
            'message' => 'Invalid file uploaded',
        ];
        die(json_encode($response));
    }
}

答案 1 :(得分:0)

您可以在邮递员的名称字段中添加一个file_name [],然后可以从中选择多个文件

您必须通过PHP代码循环上传的文件

foreach ($_FILES['fil_name'] as $key => $value) {
    // your stuff
}