how to insert multiple image in database using codeigniter?

Today, We want to share with you multiple file upload in codeigniter.In this post we will show you codeigniter multiple image upload with preview, hear for codeigniter multiple file upload drag and drop 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.

Multiple files upload in Codeigniter

file upload method

 private function upload_files($path, $post_name, $files)
    {
        $config = array(
            'upload_path'   => $path,
            'allowed_types' => 'jpg|gif|png',
            'overwrite'     => 1,                       
        );

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

        $images = array();

        foreach ($files['name'] as $key => $image) {
            $_FILES['images[]']['name']= $files['name'][$key];
            $_FILES['images[]']['type']= $files['type'][$key];
            $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
            $_FILES['images[]']['error']= $files['error'][$key];
            $_FILES['images[]']['size']= $files['size'][$key];

            $fileName = $post_name .'_'. $image;

            $images[] = $fileName;

            $config['file_name'] = $fileName;

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

            if ($this->upload->do_upload('images[]')) {
                $this->upload->data();
            } else {
                return false;
            }
        }

        return $images;
    }

Multiple files upload (Array) with CodeIgniter 2.0

<form enctype="multipart/form-data" class="jNice" accept-charset="utf-8" method="post" action="http://xxxx.dev/admin/add_estates">              
    <fieldset>      
            <label>Title * : </label>                       
            <input type="text" class="text-long" value="" name="post_name">

            <label>Description : </label>                       
            <textarea class="mceEditor" rows="10" cols="40" name="article_info"></textarea>

            <label>Image : </label>                     
            <input type="file" multiple="" name="images[]">                             

            <button class="button-submit" type="submit" name="save" id="">Save</button>
    </fieldset>         
</form>

controller

public function add_estates()
{
    $data['page_post_name'] = "© IDEAL - Administrator - Real State - Add Real Estate";
    $data['main_content'] = 'admin/add_estates';

    if ($this->input->post() !== FALSE) {           
        $this->load->library('form_validation');
        $this->form_validation->set_rules('post_name', 'Career Title', 'trim|required');           

        if ($this->form_validation->run() !== FALSE) {                

            $post_name = $this->input->post('post_name');
            $article_info = $this->input->post('article_info');

            if (!empty($_FILES['images']['name'][0])) {
                if ($this->upload_files($post_name, $_FILES['images']) === FALSE) {
                    $data['error'] = $this->upload->display_errors('<div class="alert alert-danger">', '</div>');
                }
            }                   

            if (!isset($data['error'])) {
                $this->admin_model->add_estate($post_name, $article_info, $image_name);    
                $this->session->set_flashdata('suc_msg', 'New real estate added successfully'); 
                redirect('admin/add_estates');    
            }          
        }
    }

    $data['suc_msg'] = $this->session->flashdata('suc_msg');

    $this->load->view('layout_admin', $data);
}

I hope you get an idea about multiple file upload in codeigniter using ajax.
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.