您好我在使用Laravel 5.2时偶然发现了一个问题我不知道问题是针对5.2的特定问题还是发生在框架的其他版本中。
基本上我尝试做的是通过单个html文件输入字段上传多个文件,这就是我所拥有的。
观点:
<form method="post" action="nieuw" enctype="multipart/form-data">
<input type="file" class="form-control" name="images" multiple>
</form>
请求文件:
public function rules()
{
return [
'images' => 'mimes:jpg,jpeg,png'
];
}
最后但并非最不重要的是控制器:
public function store(Requests\EventRequest $request)
{
dd($request->file('images'));
}
现在,当通过输入字段上传多个文件时,这就是转储显示的内容。
UploadedFile {#30 ▼
-test: false
-originalName: "anotherone.jpg"
-mimeType: "image/jpeg"
-size: 64112
-error: 0
path: "/tmp"
filename: "phpm57pCe"
basename: "phpm57pCe"
pathname: "/tmp/phpm57pCe"
extension: ""
realPath: "/tmp/phpm57pCe"
aTime: 2016-02-25 23:46:57
mTime: 2016-02-25 23:46:57
cTime: 2016-02-25 23:46:57
inode: 1443315
size: 64112
perms: 0100600
owner: 900
group: 900
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}
它似乎只是通过上传字段捕获您提交的最后一个文件。请注意,请求验证程序此时可以正常工作,您只能上传设置的mime类型,但其请求无法显示所有文件。
我自己想到的是将html输入字段设置为像这样的数组
<form method="post" action="nieuw" enctype="multipart/form-data">
<input type="file" class="form-control" name="images
[]" multiple>
</form>
解决了控制器端的问题,使转储看起来像这样
<form method="post" action="nieuw" enctype="multipart/form-data">
<input type="file" class="form-control" name="images
然而,通过这种方法,验证已完全消失,我尝试将验证器更改为:
" multiple>
</form>
进入
array:2 [▼
0 => UploadedFile {#30 ▼
-test: false
-originalName: "test - kopie.jpg"
-mimeType: "image/jpeg"
-size: 64112
-error: 0
path: "/tmp"
filename: "phpj3V7H1"
basename: "phpj3V7H1"
pathname: "/tmp/phpj3V7H1"
extension: ""
realPath: "/tmp/phpj3V7H1"
aTime: 2016-02-25 23:52:21
mTime: 2016-02-25 23:52:21
cTime: 2016-02-25 23:52:21
inode: 1443314
size: 64112
perms: 0100600
owner: 900
group: 900
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}
1 => UploadedFile {#31 ▼
-test: false
-originalName: "anotherone - kopie.jpg"
-mimeType: "image/jpeg"
-size: 4721735
-error: 0
path: "/tmp"
filename: "phpWSm5U9"
basename: "phpWSm5U9"
pathname: "/tmp/phpWSm5U9"
extension: ""
realPath: "/tmp/phpWSm5U9"
aTime: 2016-02-25 23:52:21
mTime: 2016-02-25 23:52:21
cTime: 2016-02-25 23:52:21
inode: 1443315
size: 4721735
perms: 0100600
owner: 900
group: 900
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}
]
他们两人都无可救药地失败了。我希望你们中的一些人可以帮助我。提前谢谢!
答案 0 :(得分:1)
没有验证array
iamges你可以做什么
$validator = \Validator::make($request->all(), [
'images' => 'required'
]);
$files = $request->file('images');
foreach ($files as $file){
$validator->after(function($validator) use ($file) {
//make your valdiation
});
}
if ($validator->fails()) {
//validation didn't pass
}
另一种方式
$files = $request->file('images');
foreach ($files as $file){
$input = ['upload' => $files[$i]];
$rules = ['upload' => 'image|max:15500'];
$validation = Validator::make($input, $rules);
if($validator->fails()){
//validation didn't pass
break;
}
}
第三种方法是创建自己的Validation rule
答案 1 :(得分:1)
您可以像 Laravel 5.2 中的任何输入数组一样验证文件数组。此功能是Laravel 5.2中的新功能。你可以这样做:
$input_data = $request->all();
$validator = Validator::make(
$input_data, [
'image_file.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'image_file.*.required' => 'Please upload an image',
'image_file.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'image_file.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);
if ($validator->fails()) {
// Validation error..
}
注意,您也可以在控制器中使用$this->validate($request, $rules, $messages);
。