uploading multiple files using one file field in codeigniter
SOLVED
I have a file field named additional_photos[]
on a web page, there can be n number of instances of this field of course with different ID but same name. In a normal PHP code I will do a foreach
on $_FILES['additional_photos']
and will do rest easily. But how do I achieve the same with CodeIgniter? I tried doing this:
$additional_photosCount = count($_FILES['additional_photos']['name']); //Is there a better way to refer $_FILES like I can refere $_POST in CI?
for($i=0; $i< $additional_photosCount; $i++){
$uploadConfig['file_name'] = $this->properties['userId'].'-'.$_FILES['additional_photos']['name'][$i];
$this->CI->upload->initialize($uploadConfig);
if(!$this->CI->upload->do_upload('additional_photos['.$i.']')){;
echo $this->CI->upload->display_errors();
}
}
But this,
a) IMHO, isn't correct way b) gives me error "You did not select a file to upload." Update This is a way out I could apply: $additional_photosCount = count($_FILES['additional_photos']['name']);
for($i=0; $i&l开发者_如何学JAVAt; $additional_photosCount; $i++){
$uploadConfig['file_name'] = $this->properties['userId'].'-'.$_FILES['additional_photos']['name'][$i];
$this->CI->upload->initialize($uploadConfig);
$_FILES['additional_photos_'.$i] = array(
'tmp_name'=> $_FILES['additional_photos']['tmp_name'][$i],
'name'=> $_FILES['additional_photos']['name'][$i],
'type'=> $_FILES['additional_photos']['type'][$i],
'size'=> $_FILES['additional_photos']['size'][$i],
'error'=> $_FILES['additional_photos']['error'][$i],
);
if(!$this->CI->upload->do_upload('additional_photos_'.$i)){;
echo $this->CI->upload->display_errors();//TODO: instead of echoing push errors to a list
}
}
Check this : https://github.com/nicdev/CodeIgniter-Multiple-File-Upload , should do what you want above.
in HTML5 you can use this functionality
If you want to let the user select multiple files, simply use the multiple attribute on the input element:
<input type="file" id="input" multiple onchange="handleFiles(this.files)">
REFERENCE
精彩评论