Is the file input called image or photos - you are using both in the validation and controller action logic. Validation will always fail if your input name is photos because image is not in the request
Oct 2, 2019
14
Level 3
Laravel | File upload validation
Hello again guys, I am trying to validate image upload with code:
PhotoController function store:
public function store(Request $request)
{
request()->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->has('photos')){
foreach ($request->photos as $photo) {
$filename = 'img-'.time().'.'.$photo->getClientOriginalExtension();
$photo->storeAs('public/photos/',$filename);
Photo::create([
'filename' => $filename
]);
}
}
return redirect()->action('PhotoController@plist');
}
Part of blade's view:
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong>
There were problems adding the file.
<ul>
@foreach ($errors->all() as $error)
<li>The size of a single photo should not exceed 1024KB.</li>
@endforeach
</ul>
</div>
@endif
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
The error appears even when I try to upload a file with a size of 50KB. I am using XAMPP localhost server.
Please or to participate in this conversation.