我的代码可以上传到单个文件但无法上传多个文件请告诉我我的错误。
以下是错误消息。
数组([error] => 您没有选择要上传的文件。
)
更新我的问题 当我做的时候
echo $i.":".$_FILES['file_upload']['name'][$i].'<br/>';
结果如下
0:标题标题-bg.jpg 1:varun.jpg
但在我将代码更改为
之后$_FILES['file_upload']['name'] = $_FILES['file_upload']['name'][$i];
echo $i.":".$_FILES['file_upload']['name'].'<br/>';
结果如下:
0:标题标题-bg.jpg 1:e
// CONTROLLER
public function addPhoto(){
if (!empty($_FILES)):
$count = count($_FILES['file_upload']['name']);
for($i =0; $i<$count;$i++):
$_FILES['file_upload']['name'] = $_FILES['file_upload']['name'][$i];
$_FILES['file_upload']['type'] = $_FILES['file_upload']['type'][$i];
$_FILES['file_upload']['tmp_name'] = $_FILES['file_upload']['tmp_name'][$i];
$_FILES['file_upload']['error'] = $_FILES['file_upload']['error'][$i];
$_FILES['file_upload']['size'] = $_FILES['file_upload']['size'][$i];
$config['upload_path'] = './uploads/employee/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 5000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_upload')){
$data = $this->upload->data();
echo "<pre>";
print_r($data);
echo "</pre>";
}else{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
endfor;
endif; //$_FILE
}
查看
<input type="file" name="file_upload[]" class="form-control-file" id="fileUpload" multiple>
答案 0 :(得分:0)
这是我项目中的工作代码
$data = array();
$filesCount = count($_FILES['daily_records']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['daily_record']['name'] = $time."-".$_FILES['daily_records']['name'][$i];
$_FILES['daily_record']['type'] = $_FILES['daily_records']['type'][$i];
$_FILES['daily_record']['tmp_name'] = $_FILES['daily_records']['tmp_name'][$i];
$_FILES['daily_record']['error'] = $_FILES['daily_records']['error'][$i];
$_FILES['daily_record']['size'] = $_FILES['daily_records']['size'][$i];
$uploadPath = './assets/uploads/daily_records/daily_record';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png|pdf|docx';
//$config['max_size'] = '100';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('daily_record')){
$fileData = $this->upload->data();
$file_name = $fileData['file_name'];
echo $file_name;
}
}
答案 1 :(得分:0)
我已经自己解决了。
这是我的最终代码,现在对我有用。
foreach ($_FILES as $key => $v) {
for ( $i = 0; $i < count($v['name']); $i++ ){
$_FILES['file_upload']['name'] = $v['name'][$i];
$_FILES['file_upload']['type'] = $v['type'][$i];
$_FILES['file_upload']['tmp_name'] = $v['tmp_name'][$i];
$_FILES['file_upload']['error'] = $v['error'][$i];
$_FILES['file_upload']['size'] = $v['size'][$i];
//echo $_FILES['file_upload']['name'] ;
if(! $this->upload->do_upload('file_upload')){
$error = array('error' => $this->upload->display_errors());
print_r($error);
}else{
$data = $this->upload->data();
echo "<pre>";
print_r($data);
echo "</pre>";
}
}
}