Multiple files upload (Array) with CodeIgniter 2.0
I finally managed to make it work with your help!
Here's my code:
function do_upload(){ $this->load->library('upload'); $files = $_FILES; $cpt = count($_FILES['userfile']['name']); for($i=0; $i<$cpt; $i++) { $_FILES['userfile']['name']= $files['userfile']['name'][$i]; $_FILES['userfile']['type']= $files['userfile']['type'][$i]; $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_FILES['userfile']['error']= $files['userfile']['error'][$i]; $_FILES['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options()); $this->upload->do_upload(); }}private function set_upload_options(){ //upload an image options $config = array(); $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '0'; $config['overwrite'] = FALSE; return $config;}
Thank you guys!
You should use this library for multi upload in CI https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Installation Simply copy the MY_Upload.php file to your applications library directory.
Use: function test_up in controller
public function test_up(){if($this->input->post('submit')){ $path = './public/test_upload/'; $this->load->library('upload'); $this->upload->initialize(array( "upload_path"=>$path, "allowed_types"=>"*" )); if($this->upload->do_multi_upload("myfile")){ echo '<pre>'; print_r($this->upload->get_multi_upload_data()); echo '</pre>'; }}else{ $this->load->view('test/upload_view');}
}
upload_view.php in applications/view/test folder
<form action="" method="post" enctype="multipart/form-data"><input type="file" name="myfile[]" id="myfile" multiple><input type="submit" name="submit" id="submit" value="submit"/>
Try this code.
It's working fine for me
You must initialize each time the library
function do_upload() { foreach ($_FILES as $index => $value) { if ($value['name'] != '') { $this->load->library('upload'); $this->upload->initialize($this->set_upload_options()); //upload the image if ( ! $this->upload->do_upload($index)) { $error['upload_error'] = $this->upload->display_errors("<span class='error'>", "</span>"); //load the view and the layout $this->load->view('pages/uploadform', $error); return FALSE; } else { $data[$key] = array('upload_data' => $this->upload->data()); $this->load->view('pages/uploadsuccess', $data[$key]); } } } } private function set_upload_options() { //upload an image options $config = array(); $config['upload_path'] = 'your upload path'; $config['allowed_types'] = 'gif|jpg|png'; return $config; }
Further edit
I have found the way you must upload your files with one unique input box
CodeIgniter doesn't support multiple files. Using the do_upload() in a foreach won't be different than using it outside.
You will need to deal with it without the help of CodeIgniter. Here's an example https://github.com/woxxy/FoOlSlide/blob/master/application/controllers/admin/series.php#L331-370
This is that said you in the commments :)