CodeIgniter Multiple File Upload not working CodeIgniter Multiple File Upload not working codeigniter codeigniter

CodeIgniter Multiple File Upload not working


Try like this,

function upload_files(){           $config = array();    $config['upload_path'] = './Images/';    $config['allowed_types'] = 'gif|jpg|png';    $config['max_size']      = '0';    $config['overwrite']     = FALSE;    $this->load->library('upload');    $files = $_FILES;    for($i=0; $i< count($_FILES['userfile']['name']); $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($config);        $this->upload->do_upload();    }}


this is working

 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;    }


I'm going to add my answer as a fuller explanation to @SachinMarwa's own answer. The code I am submitting is not different code, but rather adds some lines and specifics not mentioned in his answer.

Even though it looks like his answer is technically correct, and perhaps the answers above his are also correct in their own way, they didn't work for me. I had to study this problem to find out what is really going on, and I learned enough about the process to understand how to write my own solution.

First of all, refer to Nana Partykar's comment, "In your controller, i'm not able to see any is_uploaded_file() function ?" That comment tells us that people are misunderstanding the two files which have similar names, but are different. I know, because for a while I thought they must be referring to the same file, the controller file (named "Uploader.php"). I can see almost all of these questions refer to the same "How to upload multiple files using Ajax" tutorial somewhere out there, my own version, included. The code we are all using 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 is referring to a system/library module file named "Upload.php". Notice that before you call the do_upload() function, you have to invoke this line: $this->load->library('upload', $config);

Sachin Marwha gave us a for loop that loops through the $_FILES['userfile'] array. Let's say you upload three pictures. Each $_FILES['userfile'] element is itself made up of 5 "properties": name, type, tmp_name, error, size. You can see these $_FILE properties on PHP.

You only want to pass one file at a time to do_upload(). You don't want to pass all three (or even 20) files at a time to do_upload. That means you have to break down the $_FILES['userfile'] array into individual files before you call do_upload(). To do this, I make a $_FILES['f'] element of the $_FILES array. 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 was getting the infamous “did not select a file to upload” that everybody (including myself) keeps complaining about. That function, you will find, uses the original $_FILES array your form sends to your controller. But it really only uses the name of the input type=file from your form. If you don't tell it the name of the form input, it will default to $_FILES['userfile']. As it turns out, that was my biggest problem, because if I used the name of my input field, well, that field passes an array or a collection of files, not just a single file. So I had to make a special $_FILES['f] element, and ONLY pass $_FILES['f'].

Here's the way I am doing it, and believe me, I tried all of the versions on this page and others, not just one StackOverflow, but other tutorials, as well:

    $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');    }        

Where it unsets the $config array within the for i loop, and then remakes the $config array, that's the part that makes the thumbnail images for each picture file.

The Complete Controller Upload 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);    }