Thanks TheFuzzy0ne, for your input.
I did not use their way because I like to use the simple html way. I rebuild the Upload script to do everything in the first post. I even made it better, you can even use a combination of multiple upload with and multiple array upload. You can even make global and file specific config. The use of the original script stays exactly the same but now you can do more than that, multiple and multiple array.
The only thing I still have to think of, is the way error handling works. For now you get the original errors with multiple upload. Which means it wont tell you which files got an error. So if you have an error in one of the uploading files you will return to the form like the original way and give the errors, but the good uploads will be done. This last thing you can think of removing the error-less files or make a choice of only uploading when all are good. I did not build in this feature. What do you think? I like the form validation class, it would be nice to have a similar functionality in the upload class as well.
Check the controller and the class, put the controller in controllers and the class in your application libraries (not system libraries) to test. The class is a CI_Upload class and will not use the default class in the system libraries folder, but overrule it.
I like people to test and give some good feedback to make the script for the community better. Get a good look at the controller, I have commented various optional methods of doing things. A lot of things are possible.
Single file solution: (standard solution and works just fine)
// Controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg';
$config['encrypt_name'] = true;
$this->load->library('upload',$config);
#if (!$this->upload->do_upload()) // default input name userfile
if (!$this->upload->do_upload('single_file')) // input name (type file)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form',$error);
}
else
{
#$data = array('upload_data' => $this->upload->data()); // default all
$data = array('upload_data' => $this->upload->data('single_file')); // [new] input name (type file)
$this->load->view('upload_success',$data);
}
// View
<form action="/upload_files/upload_single_file" method="post" enctype="multipart/form-data">
<input type="file" name="single_file" value="" />
<input type="submit" name="" value="Upload" />
</form>
Multiple file solution: (three file example)
// Controller
// Global
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg';
$config['encrypt_name'] = true;
// File specific (overrule global and optional)
$config['file_2']['upload_path'] = './uploads/other/';
$config['file_2']['allowed_types'] = 'doc|pdf';
$config['file_2']['encrypt_name'] = true;
$this->load->library('upload',$config);
#if (!$this->upload->do_upload()) // default input name userfile[]
#if (!$this->upload->do_upload('file_1,file_2,file_3')) // csv of input names (type file)
if (!$this->upload->do_upload(array('file_1','file_2','file_3'))) // array of input names (type file)
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form',$error);
}
else
{
#$data = array('upload_data' => $this->upload->data()); // default all
#$data = array('upload_data' => $this->upload->data('file_1,file_2,file_3')); // csv
$data = array('upload_data' => $this->upload->data(array('file_1','file_2','file_3'))); // array
$this->load->view('upload_success',$data);
}
// View
<form action="/upload_files/upload_multiple_files" method="post" enctype="multipart/form-data">
<input type="file" name="file_1" value="" />
<input type="file" name="file_2" value="" />
<input type="file" name="file_3" value="" />
<input type="submit" name="" value="Upload" />
</form>
Multiple file array solution: (three file example, see attachement for jquery version)
// Controller
// config and handling exactly the same as single file example
// the output of data is different
Array
(
[file_name] => Array
(
[0] => 8dd9faf4e710e12fec549019183e486a.JPG
[1] => d0d1ee59d28db82ce4a92418ac655642.JPG
[2] => 28feb3672a5fbcc6b5745f52f574d034.JPG
)
...
// View
<form action="/upload_files/upload_array_files" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" value="" />
<input type="file" name="files[]" value="" />
<input type="file" name="files[]" value="" />
<input type="submit" name="" value="Upload" />
</form>
I hope someone have some good feedback and I hope that this method or something like it will be default in the next CI versions. I also hope that someone can make the error validation something like form validation.
If you find some bugs, feel free to post them.
The class is a rewrite of the 1.7.1 version.
Mike