我有点问题。当我选择上传一张图片时,功能起作用。但是对于多个图像它不起作用,我得到错误
您没有选择要上传的文件。
(如果用户输入正确的代码,则通过JS创建更多图像上传的字段。如果未输入代码或输入错误,则使用原始输入来选择文件)。似乎有什么问题?
控制器功能:
function img_upload($folder) {
$this->path = './public/img/' . $folder;
$imgs = array();
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->path
);
$CI = &get_instance();
$CI->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!$CI->upload->do_upload($key)) {
return $error = implode(',',array('error' => $CI->upload->display_errors()));
} else {
$q = $CI->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $this->path . '/' . $q['file_name'];
$config['new_image'] = $this->path . '/thumbs';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 128;
$config['height'] = 128;
$CI->load->library('image_lib');
$CI->image_lib->clear();
$CI->image_lib->initialize($config);
$CI->image_lib->resize();
array_push($imgs, $q['file_name']);
}
}
if(empty($imgs)){
return FALSE;
} else {
return implode(',', $imgs);
}
}
用于选择文件的 HTML 的一部分:
<label for="image">Slika</label><input type="file" name="userfile" id="image" />
用于添加新字段的 JS:
code.on('focusout', function(){
if(!$(this).val())
{
$('label[for^=image_paid], input[id^=image_paid], input[name=time]').remove();
}
if(!$('#image4').length > 0)
{$.ajax({
type: 'POST',
url: '<?php echo base_url() ?>global_info/gi_get_ad_payment_code',
data: 'code=' + code.val(),
success: function(data){
for(i = 1; i<=4; i++)
{
$('#image').after('<label for="image' + i +'">Slika</label><input type="file" name="userfile" id="image' + i +'" />');
}
code.after('<input type="hidden" name="time" value="'+ data +'" />');
code.after('<input type="hidden" name="paid" value="1" />');
},
error: function(){
alert('nije uspeh');
}
}); /* KRAJ NA AJAX */
};/* KRAJ NA IF */
});