Multiple files upload (Array) with CodeIgniter 2.0

I've been searching and struggling for 3 days now to make this works but I just can't. What I want to do is use a Multiple file input form and then upload them. I can't just use a fixed number of file to upload. I tried many many solutions on StackOverflow but I wasn't able to find a working one.

Here's my Upload controller

<?php

class Upload extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url','html'));
}

function index()
{    
    $this->load->view('pages/uploadform', array('error' => ' ' ));
}

function do_upload()
{
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png';


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

 foreach($_FILES['userfile'] as $key => $value)
    {

        if( ! empty($key['name']))
        {

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload($key))
            {
                $error['error'] = $this->upload->display_errors();

                $this->load->view('pages/uploadform', $error);
            }    
            else
            {
                $data[$key] = array('upload_data' => $this->upload->data());

                $this->load->view('pages/uploadsuccess', $data[$key]);


            }
         }

    }    
  }    
 }
 ?> 

My upload form is This.

 <html>
 <head>
    <title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" multiple name="userfile[]" size="20" />
<br /><br />


<input type="submit" value="upload" />

</form>

</body>
</html> 

I just keep having this error :

You did not select a file to upload.

Here's the array of the example:

Array ( [userfile] => Array ( [name] => Array ( [0] => youtube.png [1] => zergling.jpg ) [type] => Array ( [0] => image/png [1] => image/jpeg ) [tmp_name] => Array ( [0] => E:\wamp\tmp\php7AC2.tmp [1] => E:\wamp\tmp\php7AC3.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 35266 [1] => 186448 ) ) )

I have this like 5 times in a row if I select 2 files. I also use the standard Upload library.


Solution 1:

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!

Solution 2:

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"/>

Solution 3:

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

https://stackoverflow.com/a/9846065/1171049

This is that said you in the commments :)

Solution 4:

another bit of code here:

refer: https://github.com/stvnthomas/CodeIgniter-Multi-Upload

Solution 5:

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

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