
Multiple file upload in Codeigniter with example.
Codeigniter doesn't have a support for upload multiple files at once; they saw it was out of framework philosophy, and because we don’t have the time to argue about this, we just make it by hands.
Html5 and jQuery give you the ability to upload multiple files, not just that, also to make it easy for user to control uploading files extensions by validating before upload.
In this tutorial we will learn how to do this :
1- Using another improved Codeigniter file upload library
We should inherit and hack Codeigniter file upload library to give it the ability to upload multiple files with single input, although we can do this task without any other library but for code clean and to avoid repetitions I recommended to use this library
This library doesn't change anything to write your code, so if you have already build your project with regular codeigniter file upload library you will not have to change the original uploading code, this library will detect single or multi upload, your turn to handle inserting files information to your table;
2-Using jQuery MultiFile Plugin
This light Wight jQuery Plugin helps you to easily select multiple files for upload. It lets you do some basic validations before files uploaded, based on their extension or size, I use it to make simple Codeigniter upload file example without any complex features
To select multiple files at once you should define some attributes, other optional Use HTML5's multiple attribute
class="multi" to active the plugin and class="with-preview"
maxlength="10" Limit: 10 files
accept="gif|jpg|png Allowed extensions: gif, jpg, png.
data-maxfile="1024" Allowed files sizes (under <1MB each) or data-maxfile="1024" Allowed files sizes (under <1MB in total)
type="file" to define input type
<input
multiple
class="multi with-preview"
maxlength="10"
accept="gif|jpg|png"
data-maxfile="1024"
/>
name="files[]" .. Important if you want to make it multi upload input to add [], these will define input as multidimensional array as example to see array like this
Array
(
[0] => Array
(
[name] => foo.txt
[type] => text/plain
[tmp_name] => /tmp/phpYzdqkD
[error] => 0
[size] => 123
)
[1] => Array
(
[name] => bar.txt
[type] => text/plain
[tmp_name] => /tmp/phpeEwEWG
[error] => 0
[size] => 456
)
)
enctype="multipart/form-data" .. It is an encoding type that allows files to be sent through a POST. And without this encoding the files cannot be sent through POST.
If you want to upload a file via a form, you must use this attribute in the form.For full documentation you can see this page
Project files
1- Plugin files in Global/ Multifile path
2- Library file path libraries/My_upload.php