CodeIgniter - uploading multiple files with one in

2019-02-21 01:20发布

I'm trying to upload several files at once from ONE input:

<input type="file" name="form-image[]" id="form-image" multiple="true" />

Which itself without CodeIgniter works fine. In my attempt to do the same thing with CodeIgniters file helper; I get an error (is_uploaded_file() expects parameter 1 to be string, array given), which indicates that I am doing something wrong, or CodeIgniter simply doesn't support multiple files?

My faulty code so far:

//Iterate through each file uploaded
for ($i = 0; isset($_FILES['form-image']['name'][$i]); $i++) {

   //Fix the settings
   $config['upload_path'] = "./static/vimmel/";
   $config['file_name'] = time() . rand(1,988);
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size']  = '1024';
   $config['max_width']  = '1024';
   $config['max_height']  = '768';
   $this->load->library('upload', $config);

   //Do the upload
   $this->upload->do_upload("form-image")
}

Please note that I did edit out all error handling and such. Also note that I do not use the is_uploaded_file() function.

Appreciating all help and suggestions. Thanks.

2条回答
forever°为你锁心
2楼-- · 2019-02-21 01:24

I'm using an extension of the Upload library that includes a do_multiple_upload() function accepting an array of files

https://github.com/nicdev/CodeIgniter-Multiple-File-Upload

It lacks some error management (e.g. if one of your files doesn't have an allowed extension the whole upload fails), but you can change the returns for continues in the loop.

Hope it helps somebody...

查看更多
冷血范
3楼-- · 2019-02-21 01:33

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

查看更多
登录 后发表回答