Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

Use: function test_up in controller

upload_view.php in applications/view/test folder

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


/**
 * @return array an array of your files uploaded.
 */
private function _upload_files($field='userfile'){
    $files = array();
    foreach( $_FILES[$field] as $key => $all )
        foreach( $all as $i => $val )
            $files[$i][$key] = $val;

    $files_uploaded = array();
    for ($i=0; $i < count($files); $i++) { 
        $_FILES[$field] = $files[$i];
        if ($this->upload->do_upload($field))
            $files_uploaded[$i] = $this->upload->data($files);
        else
            $files_uploaded[$i] = null;
    }
    return $files_uploaded;
}
<input type="file" multiple name="images[]" size="20" />
<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">
public function do_upload(){
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';
    //...

    $this->load->library('upload',$config);

    if ($_FILES['images']) {
        $images= $this->_upload_files('images');
        print_r($images);
    }
}

in the controller:

in your case:

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

CodeIgniter doesn't support multiple files. Using the do_upload() in a foreach won't be different than using it outside.

I added a further edit in the answer with the how to

I found the problem, you need another interation on the foreach, because the format of $_FILES when uses a multiple upload input is different. For example: [name] => Array ( [0] => youtube.png [1] => zergling.jpg ). Is not a array of arrays is one array with the properties as array

I have found the way you must upload your files with one unique input box

So i need a function to sort of modify my "array" into a good one and then it will work right ?

This is that said you in the commments :)

You must initialize each time the library

it still doesn't work for me. With your function and my view (quoted above) i get > is_uploaded_file() expects parameter 1 to be string, array given in the library file Upload.php (line 161) I change the upload path of course. And it still tell me > no file selected

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

CinetiK, if our help was useful (you have found the solution through my answer) you must mark our answer is useful!! The solution is in my answer!!

This code is also the only way I can get it to work. However it's so ugly that one need to convert the array values... Should be a more beautiful solution! Possibly extending the ul library?

Works for me. is there any way to make a table of all the selected files?

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

CinetiK, if our help was useful (you have found the solution through my answer) you must mark our answer is useful!! The solution is in my answer!!

This code is also the only way I can get it to work. However it's so ugly that one need to convert the array values... Should be a more beautiful solution! Possibly extending the ul library?

Works for me. is there any way to make a table of all the selected files?

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


/**
 * @return array an array of your files uploaded.
 */
private function _upload_files($field='userfile'){
    $files = array();
    foreach( $_FILES[$field] as $key => $all )
        foreach( $all as $i => $val )
            $files[$i][$key] = $val;

    $files_uploaded = array();
    for ($i=0; $i < count($files); $i++) { 
        $_FILES[$field] = $files[$i];
        if ($this->upload->do_upload($field))
            $files_uploaded[$i] = $this->upload->data($files);
        else
            $files_uploaded[$i] = null;
    }
    return $files_uploaded;
}
<input type="file" multiple name="images[]" size="20" />
<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">
public function do_upload(){
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';
    //...

    $this->load->library('upload',$config);

    if ($_FILES['images']) {
        $images= $this->_upload_files('images');
        print_r($images);
    }
}

in the controller:

in your case:

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

CinetiK, if our help was useful (you have found the solution through my answer) you must mark our answer is useful!! The solution is in my answer!!

This code is also the only way I can get it to work. However it's so ugly that one need to convert the array values... Should be a more beautiful solution! Possibly extending the ul library?

Works for me. is there any way to make a table of all the selected files?

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

Use: function test_up in controller

having problem with this method. something wrong in the do_multi_upload function. doesnt return anything. blank screen error

upload_view.php in applications/view/test folder

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


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

CodeIgniter doesn't support multiple files. Using the do_upload() in a foreach won't be different than using it outside.

I added a further edit in the answer with the how to

I found the problem, you need another interation on the foreach, because the format of $_FILES when uses a multiple upload input is different. For example: [name] => Array ( [0] => youtube.png [1] => zergling.jpg ). Is not a array of arrays is one array with the properties as array

I have found the way you must upload your files with one unique input box

So i need a function to sort of modify my "array" into a good one and then it will work right ?

This is that said you in the commments :)

You must initialize each time the library

it still doesn't work for me. With your function and my view (quoted above) i get > is_uploaded_file() expects parameter 1 to be string, array given in the library file Upload.php (line 161) I change the upload path of course. And it still tell me > no file selected

Note
Rectangle 27 1

Multiple files upload (Array) with CodeIgniter 2.0?


$files = $_FILES;

for($i=0; $i<count($files['userfile']['name']); $i++)
{
    $_FILES = array();
    foreach( $files['userfile'] as $k=>$v )
    {
        $_FILES['userfile'][$k] = $v[$i];                
    }

    $this->upload->do_upload('userfile')
}

As Carlos Rincones suggested; don't be affraid of playing with superglobals.

Note