I am going to add my answer as a more complete explanation for @SachinMarwa's own answer. The code I present is no different from the code, but it adds some lines and features not mentioned in his answer.
Although it seems that his answer is technically correct, and perhaps the answers above him are also true in their own way, they did not work for me. I had to study this problem to find out what was really going on, and I learned the process enough to understand how to write my own solution.
First of all, refer to the comment of Nana Partykar: "In your controller, I do not see any function is_uploaded_file ()?" This comment tells us that people don’t understand two files that have similar names but are different from each other. I know, because for some time I thought that they should refer to the same file, the controller file (named "Uploader.php"). I see that almost all of these questions relate to the same “How to upload multiple files using Ajax” somewhere out there, including my own version. The code we all use is exactly the same.
But the controller file is "Uploader.php". Where you see $ this-> upload-> do_upload () or $ this-> upload-> do_upload ('userfile') or even $ this-> upload-> do_upload ('files'), this refers to the system / library module named "Upload.php". Please note that before calling the do_upload () function, you must call this line: $ this-> load-> library ('upload', $ config);
Sachin Marwha provided us with a for loop that traverses the $ _FILES ['userfile'] array. Say you upload three photos. Each element of $ _FILES ['userfile'] itself consists of 5 "properties": name, type, tmp_name, error, size. You can see these properties of $ _FILE in PHP .
You want to transfer only one file at a time before do_upload (). You do not want to transfer all three (or even 20) files at a time before do_upload. This means that you must split the $ _FILES ['userfile'] array into separate files before you call do_upload (). For this, I am making the element $ _FILES ['f'] of the array $ _FILES. I figured this out by setting breakpoints in the system / library / Upload.php file in the do_upload ($ file = 'userfile') function to see where I get the infamous “didn’t select the file to upload” so that everything (including me) keeps complaining. This function, you will find, uses the original $ _FILES array, which your form submits to your controller. But it really only uses a name like input = file from your form. If you do not specify a form input name, it will default to $ _FILES ['userfile']. As it turned out, this was my biggest problem, because if I used the name of the input field, then this field transferred an array or a collection of files, and not just one file. So I had to create a special element $ _FILES ['f] and ONLY pass $ _FILES [' f '].
That's how I do it, and believe me, I tried all the versions on this page and others, not just one StackOverflow, but also other tutorials:
$cpt = count($_FILES['userfile']['name']); for($i=0; $i < $cpt; $i++) { unset($config); $config = array(); $config['upload_path'] = $path; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1000'; $config['overwrite'] = TRUE; $config['remove_spaces'] = FALSE; $config['file_name'] = $_FILES['userfile']['name'][$i]; // Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop. $_FILES['f']['name'] = $_FILES['userfile']['name'][$i]; $_FILES['f']['type'] = $_FILES['userfile']['type'][$i]; $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i]; $_FILES['f']['error'] = $_FILES['userfile']['error'][$i]; $_FILES['f']['size'] = $_FILES['userfile']['size'][$i]; $this->load->library('upload', $config); $this->upload->initialize($config); if (! $this->upload->do_upload('f')) { $data['errors'] = $this->upload->display_errors(); } else { $data['errors'] = "SUCCESS"; } unset($config); $config = array(); $config['image_library'] = 'gd2'; $config['source_image'] = $path . $_FILES['userfile']['name'][$i]; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['thumb_marker'] = '.thumb'; $config['width'] = 100; $config['height'] = 100; $this->load->library('image_lib', $config); $this->image_lib->clear(); $this->image_lib->initialize($config); $this->image_lib->resize(); $types = array('.jpg'); }
If it disconnects the $ config array in a for i loop, and then redo the $ config array, then the part that makes the thumbnails for each image file.
Full controller boot function:
public function upload_asset_photo() { $data = array(); $dateArray = explode("/",$this->input->post('date')); $date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day $cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys. $padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003" $path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path if (!is_dir($path)) { mkdir($path,0755,true); //makes the ile path, if it doesn't exist } $cpt = count($_FILES['userfile']['name']); for($i=0; $i < $cpt; $i++) { unset($config); $config = array(); $config['upload_path'] = $path; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1000'; $config['overwrite'] = TRUE; $config['remove_spaces'] = FALSE; $config['file_name'] = $_FILES['userfile']['name'][$i]; $_FILES['f']['name'] = $_FILES['userfile']['name'][$i]; $_FILES['f']['type'] = $_FILES['userfile']['type'][$i]; $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i]; $_FILES['f']['error'] = $_FILES['userfile']['error'][$i]; $_FILES['f']['size'] = $_FILES['userfile']['size'][$i]; $this->load->library('upload', $config); $this->upload->initialize($config); if (! $this->upload->do_upload('f')) { $data['errors'] = $this->upload->display_errors(); } else { $data['errors'] = "SUCCESS"; } unset($config); $config = array(); $config['image_library'] = 'gd2'; $config['source_image'] = $path . $_FILES['userfile']['name'][$i]; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = TRUE; $config['thumb_marker'] = '.thumb'; $config['width'] = 100; $config['height'] = 100; $this->load->library('image_lib', $config); $this->image_lib->clear(); $this->image_lib->initialize($config); $this->image_lib->resize(); $types = array('.jpg'); } header('Content-Type: application/json'); echo json_encode($data); }