1

In my page I want user to select multiple images and upload it I am saving images name in database for reference. I am successful in uploading single images in database and can also show image in view but now I have problem in uploading multiple images.

public function add_record()
{
    $this->form_validation->set_rules('category', 'category', 'required');
    $current_date = date("Y-m-d H:i:s");
    $error='';

    if($this->form_validation->run())
    {
        $image = '';
        if($_FILES['image']['name'])
        {
            if (!is_dir('/backend_assets/media/image/')) {
                mkdir('./backend_assets/media/image/', 0777, TRUE);

            }
            $config['upload_path']          = './backend_assets/media/image/';
            $config['allowed_types']        = 'gif|jpg|jpeg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);             
            if ($this->upload->do_upload('image'))
            {
                $data = $this->upload->data();
                $image = $data['file_name'];

            }else{
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                    redirect(base_url('admin/image')); 
            }

        }

        $insert_array = array(
                                'gl_cat_id' => $this->input->post('category'),
                                'gl_image'=> $image                         
                            );
        if ($this->common_model->add_records('vm_image',$insert_array))
            {
                $id = $this->db->insert_id();
                $insert_sco_details = array(
                                    'sd_ty'=>'vm_image',
                                    'sd_ty_id'=>$id,
                                    'sd_image'=>$image
                                     );
                if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))
                {
                    $this->session->set_flashdata('success','Record added successfully');
                    redirect(base_url('admin/image'));
                }else{
                    $this->session->set_flashdata('error','Error while adding record');
                    redirect(base_url('admin/image')); 
                }

            }else{
                $this->session->set_flashdata('error','Error while adding record');
                redirect(base_url('admin/image')); 
            }

    }
    $where_array = array('vm_publications.status !=' => 4);
    $data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
    $data['include'] = 'backend/image/add_image';
    $this->load->view('backend/container', $data);
}

How is it possible with above code...?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Leozz_den
  • 85
  • 8
  • Possible duplicate of [Upload multiple images with codeigniter](https://stackoverflow.com/questions/33017753/upload-multiple-images-with-codeigniter) – gabe3886 May 24 '17 at 13:08
  • not able to get solution from other .. – Leozz_den May 24 '17 at 13:13
  • The code you've shown has only got processing and uploading code for a single file. What have you tried in order to get it working with multiple files, which would make this not a duplicate? – gabe3886 May 24 '17 at 13:24

3 Answers3

1
$current_date = date("Y-m-d H:i:s");
    $error = '';
    $image = '';
    if(isset($_FILES['image']['name']))
    {
        //print_r($_FILES);
        $id = base64_decode($this->input->post('gid'));

        $filesCount = count($_FILES['image']['name']);
        $inserted = '';
        for($i = 0; $i < $filesCount; $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];

            $config['upload_path']          = './backend_assets/media/image/';
            $config['allowed_types']        = 'gif|jpg|png';

            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if($this->upload->do_upload('userFile'))
            {
                $fileData = $this->upload->data();
                $image = $fileData['file_name'];
                $insert_array = array(
                                        'gl_cat_id' => $this->input->post('category'),
                                        'gl_image'=> $image                         
                                    );

            if ($this->common_model->add_records('vm_image',$insert_array))
            {
                $id = $this->db->insert_id();
                $insert_sco_details = array(
                                            'sd_ty'=>'vm_image',
                                            'sd_ty_id'=>$id,
                                            'sd_image'=>$image
                                             );
                if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))   
                {
                    $inserted++;
                }

            }
        }
        }
       if($inserted == $filesCount)
        {   $this->session->set_flashdata('success','Images uploaded successfully');
                redirect(base_url('adminp8AamG6ueHFNGAAp/image'));
        }else{
            $this->session->set_flashdata('error','Error while adding record');
            redirect(base_url('adminp8AamG6ueHFNGAAp/image')); 
        }
    }
    $where_array = array('vm_publications.status !=' => 4);
    $data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
    $data['include'] = 'backend/image/add_image';
    $this->load->view('backend/container', $data);
Mohanish
  • 269
  • 3
  • 11
0

try below code in you add_record() function. it will helpful to you. few days ago, i have faced same problem

$files = $_FILES;
        $count = count($_FILES['image']['name']);
        for($i=0; $i<$count; $i++)  {
                $_FILES['image']['name']= $files['image']['name'][$i];
                $_FILES['image']['type']= $files['image']['type'][$i];
                $_FILES['image']['tmp_name']= $files['image']['tmp_name'][$i];
                $_FILES['image']['error']= $files['image']['error'][$i];
                $_FILES['image']['size']= $files['image']['size'][$i];
                $this->upload->initialize($this->set_upload_options());//function defination below
                $this->upload->do_upload('image');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;

       }

      $fileName = $images;

and set file upload configuration in set_upload_options function in same controller.

function set_upload_options() { 
         $config = array();
         $config['upload_path'] = PATH;
         $config['remove_spaces']=TRUE;
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size'] = '78000';
         return $config;
}
-1

First, debug the output of the $_FILES variable. This should give you an array of files that are being uploaded. Loop through them to treat each one individually.

foreach ($_FILES as $file) { // do some file processing on the $file object instead of $_FILES object. // example: instead of using 'if($_FILES['image']['name'])' // use: 'if($file['image']['name'])' }

If you want to use CI's upload class, check out their Docs here: https://www.codeigniter.com/userguide3/libraries/file_uploading.html

Webmaster G
  • 502
  • 5
  • 12