Error when uploading multiple files in Codeigniter

2019-08-16 07:33发布

I am trying to use multiple file upload in CodeIgniter, but I'm getting an error: You did not select a file to upload.

My html code:

<input type="file" id="ModelImage" name="ModelImage[]" multiple="multiple"  value=""/>

My php code:

for($i = 0; $i < count($_FILES['ModelImage']["name"]); $i++)
{
  if (!empty($_FILES['ModelImage']['name'][$i]))
  {
    $config['upload_path'] = './uploads/starmodel/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name']= "star_".$lastid."_".$i.strrchr(basename($_FILES["ModelImage"]["name"][$i]),".");
    $this->upload->initialize($config);

    if ($this->upload->do_upload('ModelImage'))
    {
      $data = $this->upload->data();
      $udata = array('ModelImage' => $config['file_name']);
      $this->db->where('ModelID', $lastid);
      $this->db->update('starmodel', $udata);
    }
    else
    {
      echo $this->upload->display_errors();
    }
  }
}

1条回答
Bombasti
2楼-- · 2019-08-16 08:26

The name parameter must be userfile

According to the CI File Uploading Documentation:

By default the upload routine expects the file to come from a form field called userfile, and the form must be a "multipart" type:

<form method="post" action="some_action" enctype="multipart/form-data" />
    <input type="file" name="userfile" size="20" />
    <input type="submit" value="upload" />
</form>

With that said, I do not believe CI's library currently supports HTML5's multiple file uploads. You should peak around CI's forums for custom libraries.

查看更多
登录 后发表回答