Reputation: 71
Hi I have stumbled across a problem while messing around with Laravel 5.2 I dont know if the problem is specific for 5.2 or occurs in other versions of the framework.
Basically what I'm trying to do is upload multiple files trough a single html file input field this is what I have.
The view:
<form method="post" action="nieuw" enctype="multipart/form-data">
<input type="file" class="form-control" name="images" multiple>
</form>
The request file:
public function rules()
{
return [
'images' => 'mimes:jpg,jpeg,png'
];
}
And last but not least the controller:
public function store(Requests\EventRequest $request)
{
dd($request->file('images'));
}
Now when uploading multiple files trough the inpute field this is what the dump shows.
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
}
It just seems to catch the last file u submitted trough the upload field. Note that the request validator does work at this point you can only upload the set mime types but its the request that fails to show all the files.
What I have figured out on my own is that setting the html input field into an array like this
<form method="post" action="nieuw" enctype="multipart/form-data">
<input type="file" class="form-control" name="images
[]" multiple>
</form>
fixes the problem on the controller end making the dump look like this
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
}
]
However trough this method validation has completely gone out the window by doing so and i have tried changing my validator into:
public function rules()
{
return [
'images.*' => 'mimes:jpg,jpeg,png'
];
}
And into
public function rules()
{
return [
'images.0' => 'mimes:jpg,jpeg,png'
];
}
Both of them hopelessly failed. I hope some of you can help me out here. Thanks in advance!
Upvotes: 2
Views: 5089
Reputation: 2051
You can validate file array like any input array in Laravel 5.2. This feature is new in Laravel 5.2. You can do like following:
$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..
}
Note, you can also use $this->validate($request, $rules, $messages);
inside a controller.
Upvotes: 1
Reputation: 4168
There is no validation for array
of iamges what you can do is
$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
}
The other way
$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;
}
}
A third way is to create your own Validation rule
Upvotes: 1