0

I want to upload multiple images in database based on category_id and vendor_id including validation using Code-igniter Here is the screenshot

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

and his upload function to upload category document

  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'];
    }

}
  • Possible duplicate of [Multiple files upload in Codeigniter](https://stackoverflow.com/questions/20113832/multiple-files-upload-in-codeigniter) – Alex Jun 27 '19 at 20:20

2 Answers2

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));
    }
}
Vijay Makwana
  • 896
  • 8
  • 23
0

You can add a file_name[] in the name field in postman, and you can choose multiple files from there

And you have to loop the uploaded files via your PHP code

foreach ($_FILES['fil_name'] as $key => $value) {
    // your stuff
}
Justin J
  • 626
  • 5
  • 12