我想codeigniter $this->upload->do_upload('file')一次上传我所有的文件。我想上传多个文件,所有的文件都被上传了,但是所有的文件都重命名为一个名字。
示例:我上传了文件名为1.png, 2.png, 3.png, 4.png的文件,上传后所有的文件都重命名为1.png。
<input type="file" ref="file" class="form-control validator" autocomplete="off" :class="{'is-invalid': formValidate.file}" name="files[]" id="files" multiple>
下面是我的vue js代码:
let formData = new FormData();
formData.append('csrf_token', csrf);
this.isUploading = true;
for( var i = 0; i < this.$refs.file.files.length; i++ ){
let file = this.$refs.file.files[i];
formData.append('files[]', file);
}
axios.post(base_url + 'Main/Process/FilesStorage/addFiles', formData,
{
headers: {
'Content-Type': 'multipart/form-data'
},
onUploadProgress: function(progressEvent) {
this.uploadPercentage = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}.bind(this)
}).then(function(){
}).catch(function(){
})
我的编码器代码:
public function addFiles(){
if($this->session->userdata('user_id') === NULL) {
redirect(base_url().'Auth/Logout');
}
$data = [];
$count = count($_FILES['files']['name']);
$count;
for($i=0;$i<$count;$i++){
if(!empty($_FILES['files']['name'][$i])){
$_FILES['file']['name'] = $_FILES['files']['name'][$i];
$_FILES['file']['type'] = $_FILES['files']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['files']['error'][$i];
$_FILES['file']['size'] = $_FILES['files']['size'][$i];
$config['upload_path'] = PROOT.'storage/';
$config['allowed_types'] = '*';
$config['max_size'] = '15000';
$config['file_name'] = $_FILES['files']['name'][$i];
$this->load->library('upload', $config);
if($this->upload->do_upload('file')){
$uploadData = $this->upload->data();
$filename = $uploadData['file_name'];
$data['totalFiles'][] = $filename;
}else{
echo $this->upload->display_errors();
}
}
}
}
仅仅删除配置[' file_name '] =带有_file美元("文件")("名字")[我]美元;它会解决你的问题。