我的代码有点问题。问题是,当我用Jquery (Front)和Laravel(后退)上传多个文件时,它只上传了一个文件,而不是所有文件。
index.blade.php (表格)
<input type="file" name="file[]" class="form-control-file file-1">
<input type="file" name="file[]" class="form-control-file file-2">index.blade.php (Ajax )
var data = new FormData();
data.append('file[0]', $('.file-1')[0].files[0]);
data.append('file[1]', $('.file-2')[0].files[0]);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: '{{ url('/layanan/file/users/store') }}',
type: 'POST',
data: data,
processData: false,
contentType: false,
success: function(response){
mprogress.end();
console.log(data);
},
});php文件逻辑@存储
$request->file[0]->move(public_path('file'), time().'.'.$request->file[0]->extension());
$request->file[1]->move(public_path('file'), time().'.'.$request->file[1]->extension());谢谢。
发布于 2020-02-08 17:55:19
我认为问题在于在这两种情况下都使用time()生成随机文件名。由于每一行可能占用不到1秒钟,time()将为两者返回相同的值,这意味着file#2将具有与file#1相同的名称,并将覆盖它。
尝试使用不同的文件名生成,可能只是静态名称,比如file1和file2,而不是time(),或者使用this thread中提到的技术之一生成随机文件名。
https://stackoverflow.com/questions/60123382
复制相似问题