Search code examples
phpcodeigniteruploadifycodeigniter-2

Codeigniter Multi File Upload with HTML5, can it be done with tools like Uploadify?


Is it possible to do a multi file uploader using just HTML5 and PHP? I was hoping I could select multiple files, store them in a loop, and just iterate through all the files and upload them to the server.

But Codeigniter only uploads the first file I selected.

foreach ($_FILES as $key => $value)
{
      $this->upload->do_upload($key['name'])
}

I have additional logic for handling stuff like upload path and file name, but that code is fine, but this loop seems to only work for the first select file and nothing else.

If it's not possible to use HTML5 and php, I will just use Uploadify, or is there a way to modify the Upload Class to support multi file uploads?

Here's my method more or less,

public function uploadScreens($id) {
$this->load->library('upload');
$config['allowed_types'] = '*';
$config['max_size'] = '45000';
$config['max_width'] = '20000';
    $config['max_height'] = '15000';
foreach ($_FILES as $key => $value)
    {
    if (!empty($key['name']))
            {
                $this->db->set('gameid', $id);
        $this->db->insert('gamescreens');
                $screenid = $this->db->insert_id();

                $this->createDirectory($screenid);
                $filename = $this->randomFilename();
                $config['file_name'] = "$filename";
                $config['upload_path'] = APPPATH . "../content/gamescreens/" . $screenid . '/';
                $this->upload->initialize($config);

                if ($this->upload->do_upload($key['name']))
                {
                    $data = $this->upload->data();
                    $sourceurl = $data["full_path"];
                    $this->db->simple_query("update gamescreens set source = '$sourceurl' Where id=$screenid");
        }
                else
                {
                    //catch eerrors
                }
            }
    }

}


Solution

  • Based on your code example, I see the following problem:

    foreach ($_FILES as $key => $value)
    {
          $this->upload->do_upload($key['name'])
    }
    

    I believe that should be $value['name'], not true?