Multiple file upload in CodeIgniter using MY_Upload – meet my library extension

Created at: January 12, 2015; Last update: January 15, 2015

The CodeIgniter’s upload library is great. But it only works for single file uploads. So I thought that an extension to allow for multiple files upload would be a nice addition to CI’s upload library. Hence, allow me to present you MY_Upload. The extension is hosted on Github. So go there and download it.

After downloading the repo, just move libraries/MY_Upload.php to your application/libraries/ directory.

How to use it:

First of all, let’s make the view for uploading multiple files:

Now, in a controller’s method, if a form was submited, we set the configuration and load the library:

As you can see, the $config array has a new key: “multi“. This key can accept one of three values:

  • ‘all’ = only upload if all files are uploaded
  • ‘halt’ = halt uploading after first encountered error
  • ‘ignore’ = ignore errors and continue uploading remaining files

Once we’ve loaded the library, we can simply call do_upload() on our input element:

Take care here: $this->upload->do_upload() returns FALSE only if an error was encountered AND:

  • either the user tried to upload only one file;
  • or the user tried to load more than one file BUT the $config[‘multi’] was set to ‘all‘.

After the upload, we can retrieve the data of the uploaded files with $this->upload->data():

Also, in case of upload errors, we can retrieve the errors with $this->upload->display_errors:

If you have any problems in implementing or you think that we can implement something new, please write a comment below. Enjoy!

2 thoughts on “Multiple file upload in CodeIgniter using MY_Upload – meet my library extension

    1. avenirer Post author

      Hello, when you load the library, inside the configuration array you should also mention a ‘multi’ key which can accept “all”, “halt” or “ignore” as value.


Leave a Reply

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

No spam? * Time limit is exhausted. Please reload CAPTCHA.