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:
<!DOCTYPE html> <html> <head> <title><?php echo $title; ?></title> </head> <body> <h1>Upload multiple files</h1> <?php echo form_open_multipart();?> <p>Upload file(s):</p> <?php echo form_upload('uploadedimages[]','','multiple'); ?> <br /> <br /> <?php echo form_submit('submit','Upload');?> <?php echo form_close();?> </body> </html>
Now, in a controller’s method, if a form was submited, we set the configuration and load the library:
$config = array( 'upload_path' => './upload/', 'allowed_types' => 'jpg|gif', 'max_size' => '2048', 'multi' => 'all' ); // load Upload library $this->load->library('upload',$config);
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:
$this->upload->do_upload('uploadedimages');
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():
echo '<pre>'; $uploaded = $this->upload->data(); print_r($uploaded); echo '</pre>';
Also, in case of upload errors, we can retrieve the errors with $this->upload->display_errors:
echo '<pre>'; print_r($this->upload->display_errors()); echo '</pre>';
If you have any problems in implementing or you think that we can implement something new, please write a comment below. Enjoy!
it give me errors like this
File: C:\xampp\htdocs\terapi\application\libraries\MY_Upload.php
Line: 31
what should i do ?
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.