Upload Multiple Files with CodeIgnitor

Coding multiple files upload in CodeIgnitor is a bit tricky. This post will help you solve this issue by using CodeIgnitor upload library and $_POST variable.

1) Create gallery/upload.php under views folder.

<div class="content-wrapper">
   <section class="content">
        <div class="row">
            <div class="col-md-12">
                 <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">Upload Images</h3>
                    </div>
                    <div class="box-body">

                        <?php if($message != NULL) { ?>
                            <div class="alert alert-warning">
                                <h4><i class="icon fa fa-warning"></i> Warning!</h4>
                                <div class="message-wrapper"><?php echo $message; ?></div>
                            </div>
                        <?php } ?>

                        <?php echo form_open_multipart('gallery/upload');?>

                            <div class="form-group has-feedback">
                                <?php echo form_label('Description'); ?> 
                                <?php echo form_input($description);?>
                            </div>
                            

                           <div class="images-wrapper">
                                <div class="form-group has-feedback">
                                    <?php echo form_label('Images'); ?>
                                    <div class="images-fields">
                                        <input type="file" multiple="multiple" name="images[]" class="form-control photo-upload-field"><br/>
                                        <input type="file" multiple="multiple" name="images[]" class="form-control photo-upload-field"><br/>
                                        <input type="file" multiple="multiple" name="images[]" class="form-control photo-upload-field"><br/>
                                        <input type="file" multiple="multiple" name="images[]" class="form-control photo-upload-field"><br/>
                                    </div>
                                    <p class="form_description">File types allowed: gif jpg jpeg png. Max width: 2048px, max height: 2048px.</p>
                                </div>
                            </div>

                           
                            <div class="form-group">
                                <div class="btn-group">
                                    <?php echo form_submit('submit', 'Upload', array('class' => 'btn btn-primary btn-flat'));?>
                                </div>
                            </div>


                        <?php echo form_close();?>

                    </div>
                </div>
             </div>
        </div>
    </section>
</div>

Remember to put “[]” after name variable to indicate multiple fields.

2) Create Gallery controller and upload method.

I add description field in upload method to demonstrate how to use file upload fields with other fields.

class Gallery extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }


    /*
    ** Upload images
    */
    public function upload(){

        /* Form Helpers */
        $this->load->helper('form');
        $this->load->library('form_validation');

        if ($this->form_validation->run() == TRUE){

            $description = $this->input->post('description');

            //Upload images
            $upload_folder = date('Ym');
            $config['upload_path']      = './uploads/'.$upload_folder;
            $config['allowed_types']    = 'gif|jpg|jpeg|png';
            $config['max_size']         = 20480;
            $config['max_width']        = 10240;
            $config['max_height']       = 10240;
            $config['file_ext_tolower'] = TRUE;


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

            if (!is_dir('./uploads/' . $upload_folder)) {
                mkdir('./uploads/' . $upload_folder, 0777, true);
            }

            if(isset($_FILES['images']) && $_FILES['images']['name'][0] != ''){
                $files_count = count($_FILES['images']['name']);
                for($i = 0; $i < $files_count; $i++){
                    $_FILES['image']['name'] = $_FILES['images']['name'][$i];
                    $_FILES['image']['type'] = $_FILES['images']['type'][$i];
                    $_FILES['image']['tmp_name'] = $_FILES['images']['tmp_name'][$i];
                    $_FILES['image']['error'] = $_FILES['images']['error'][$i];
                    $_FILES['image']['size'] = $_FILES['images']['size'][$i];

                    if($this->upload->do_upload('image')){
                        $image = $this->upload->data();
                        //insert database or other task goes here
                    } else {
                       echo $this->upload->display_errors();
                       die;
                    }
                }

            }

        } else {

            $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
            $this->data['message_type'] = 'warning';

            $this->data['description'] = array(
                        'name'        => 'description',
                        'id'          => 'description',
                        'rows'        => 5,
                        'cols'        => 60,
                        'value'       => $this->form_validation->set_value('description'),
                        'class'       => 'form-control',
                        );
         
            /* Load Template */
            $this->load->view('gallery/upload', $this->data);
        }
    }

}

Leave a Comment

Your email address will not be published. Required fields are marked *

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close