How to Upload Multiple Files with CodeIgnitor

Uploading files in CodeIgnitor is a bit tricky, but once you get the hang of it, it’s really not that bad. Assuming you have a CodeIgnitor project setup and you are using the default database. You can follow these steps to upload multiple files into the project.

Controller:

class FileManager 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('file/upload', $this->data);
		}
	}	

}

View file upload.php:

The trick here is square brackets after name: name=”images[]”.

<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>

By continuing to use the site, you agree to the use of cookies. 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