以下是通过codeigniter上传多个文件的代码,但这不起作用。它总是抛出错误"file not selected"
HTML<form name="form1" method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>index.php/dashboard/upload">
<table class="custom">
<tr> <th>Title</th> <th>Image</th> </tr>
<tr><td><input type="text" name="title[]" id="title1"/></td>
<td><input type="file" name="userfile[1][]" multiple id="userfile1" /></td>
<td><input type="button" id="addbutton" value="Add More"></td></tr>
</table>
<table class="custom1" id="repeat-div"></table>
Javascript <script>
$(document).ready(function(e) {
var i = $('#repeat-div').size();
var j = i+1;
$('#addbutton').click(function(){
$('<tr><td><input type="text" name="title[]" id="title'+j+'"/></td><td><input type="file" id="userfile'+j+'" name="userfile['+j+'][]" multiple /></td></td></tr>').appendTo($('#repeat-div'));
++i;
j++;
});
});
</script>
控制器function upload(){
print_r($_FILES);exit;
}
输出如下:
Array
(
[userfile] => Array
(
[name] => Array
(
[1] => Array
(
[0] => file1.pdf
[1] => file2.pdf
)
[2] => Array
(
[0] => file3.pdf
[1] => file4.pdf
)
)
[type] => Array
(
[1] => Array
(
[0] => application/pdf
[1] => application/pdf
)
[2] => Array
(
[0] => application/pdf
[1] => application/pdf
)
)
[tmp_name] => Array
(
[1] => Array
(
[0] => D:'wamp'tmp'php436C.tmp
[1] => D:'wamp'tmp'php436D.tmp
)
[2] => Array
(
[0] => D:'wamp'tmp'php436E.tmp
[1] => D:'wamp'tmp'php436F.tmp
)
)
[error] => Array
(
[1] => Array
(
[0] => 0
[1] => 0
)
[2] => Array
(
[0] => 0
[1] => 0
)
)
[size] => Array
(
[1] => Array
(
[0] => 191
[1] => 1267
)
[2] => Array
(
[0] => 98591
[1] => 73302
)
)
)
)请建议如何上传这些文件
我建议先进入文件上传类。现在关于你的问题,我知道你没有将文件字段的名称传递给do_upload()
方法。这是你代码中的修改,
foreach($_FILES['userfile']['name'][$i+1] as $file){
//your logic
if ( ! $this->upload->do_upload('images')){
$error = array('error' => $this->upload->display_errors());
exit; }
else
redirect('dashboard/failure');
}