How To Implement Multiple Image Uploading With View, Edit And Delete In CodeIgniter?

Today, We want to share with you multiple image upload in codeigniter.In this post we will show you upload multiple images in codeigniter using ajax, hear for how to upload 2 separate images in codeigniter we will give you demo and example for implement.In this post, we will learn about How To Upload Multiple Images In Codeigniter? with an example.

Upload Multiple Files and Images in CodeIgniter

Step 1: Html Part

using Html code


Step 2: PHP Code

load->library('upload');
    $dataInfo = array();
    $profiles = $_FILES;
    $cpt = count($_FILES['profilePic']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['profilePic']['name']= $profiles['profilePic']['name'][$i];
        $_FILES['profilePic']['type']= $profiles['profilePic']['type'][$i];
        $_FILES['profilePic']['tmp_name']= $profiles['profilePic']['tmp_name'][$i];
        $_FILES['profilePic']['error']= $profiles['profilePic']['error'][$i];
        $_FILES['profilePic']['size']= $profiles['profilePic']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        $this->upload->do_upload();
        $dataInfo[] = $this->upload->data();
    }

    $data = array(
        'name' => $this->input->post('pd_name'),
        'prod_image' => $dataInfo[0]['file_name'],
        'prod_image1' => $dataInfo[1]['file_name'],
        'prod_image2' => $dataInfo[2]['file_name'],
        'created_time' => date('Y-m-d H:i:s')
     );
     $result_set = $this->tbl_profiles_model->insertUser($data);
}

private function set_upload_options()
{   
    //upload an image options
    $config = array();
    $config['upload_path'] = './resources/images/profiles/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

    return $config;
}
?>

Multiple Image Upload in CodeIgniter with Example

Create MySQL Database Table

CREATE TABLE IF NOT EXISTS `profiles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

Create Directory To Upload Files
make directory uploads/profiles in the root folder

Create Controller For Files Upload
make controller Files_Upload.php in application/controllers

load->model('profiles');
    }    
    function index(){ 
        $data = array();
        if($this->input->post('submitForm') && !empty($_FILES['store_profiles']['name'])){
            $profilesCount = count($_FILES['store_profiles']['name']);
            for($i = 0; $i < $profilesCount; $i++){ $_FILES['upload_File']['name'] = $_FILES['store_profiles']['name'][$i]; $_FILES['upload_File']['type'] = $_FILES['store_profiles']['type'][$i]; $_FILES['upload_File']['tmp_name'] = $_FILES['store_profiles']['tmp_name'][$i]; $_FILES['upload_File']['error'] = $_FILES['store_profiles']['error'][$i]; $_FILES['upload_File']['size'] = $_FILES['store_profiles']['size'][$i]; $uploadPath = 'uploads/profiles/'; $config['upload_path'] = $uploadPath; $config['allowed_types'] = 'gif|jpg|png'; $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if($this->upload->do_upload('upload_File')){
                    $fileData = $this->upload->data();
                    $dpStoreImg[$i]['file_name'] = $fileData['file_name'];
                    $dpStoreImg[$i]['created'] = date("Y-m-d H:i:s");
                    $dpStoreImg[$i]['modified'] = date("Y-m-d H:i:s");
                }
            }            
            if(!empty($dpStoreImg)){
                //Insert file information into the database
                $insert = $this->profiles->insert($dpStoreImg);
                $isActiveMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
                $this->session->set_flashdata('isActiveMsg',$isActiveMsg);
            }
        }
        //Get profiles data from database
        $data['profiles'] = $this->profiles->getRows();
        //Pass the profiles data to view
        $this->load->view('profiles_upload/index', $data);
    }
}

Create Files Model
make Files.php model in application/models

db->select('id,file_name,created');
        $this->db->from('profiles');
        if($id){
            $this->db->where('id',$id);
            $query = $this->db->get();
            $result = $query->row_array();
        }else{
            $this->db->order_by('created','desc');
            $query = $this->db->get();
            $result = $query->result_array();
        }
        return !empty($result)?$result:false;
    }    
    public function insert($data = array()){
        $insert = $this->db->insert_batch('profiles',$data);
        return $insert?true:false;
    }    
}

Create Upload Files Views
make profiles_upload/index.php in application/views

session->flashdata('isActiveMsg'); ?>

  • Uploaded On

  • No File uploaded.....

Configure Auto-load

$autoload['libraries'] = array('session','database');
$autoload['helper'] = array('url');

Upload Multiple Files and Images in CodeIgniter

In CodeIgniter, you can upload multiple files and images using the File Uploading Class. Here’s an example of how to upload multiple files and images in CodeIgniter:

Create a form in your view with the input field set to type=”file” and set the name attribute as an array. For example:


  
  

In your controller, load the File Uploading Class and define the configuration settings for the upload. For example:

public function upload_files() {
  $this->load->library('upload');

  // Define the upload configuration
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|jpeg|png';
  $config['max_size'] = '2048';

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

  if (!$this->upload->do_upload('files')) {
    // Handle error if file upload fails
  } else {
    // Handle success if file upload is successful
    $uploaded_files = $this->upload->data();
    $file_names = array();
    foreach ($uploaded_files as $file) {
        $file_names[] = $file['file_name'];
    }
    // Process the uploaded files as needed
  }
}

In this example, we first load the File Uploading Class by calling $this->load->library(‘upload’). We then define the upload configuration settings, including the upload path, allowed file types, and maximum file size.

Next, we initialize the upload with the configuration settings by calling $this->upload->initialize($config).

We then check if the file upload was successful by calling $this->upload->do_upload(‘files’). If the file upload was successful, we can access the uploaded file information by calling $this->upload->data(). We loop through the array of uploaded files and extract the file names, which we can then use to process the uploaded files as needed.

If the file upload fails, we can handle the error in the appropriate way based on your requirements.

Note that you may need to adjust the upload path and other configuration settings to match the requirements of your application.

I hope you get an idea about how to upload two different files in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment