With reference to the previously answered stackoverflow question that is being asked here Get filename of failed uploaded file in Laravel 5.4 I have implemented the same but I am not getting the failed uploaded filename. Please if someone can help me where I am doing wrong
$validationArray = [];
$messages = [];
if($request->hasfile('uploads')){
// dd($request->file('uploads'));
foreach ($request->file('uploads') as $key => $file) {
$validationArray['uploads.'.$key.'.image'] = 'The ' . $file->getClientOriginalName() . ' must be an image.';
}
$messages = [
$validationArray
];
}
$request->validate([
// 'customer_name' => 'required|exists:users,id',
'uploads' => 'required|array',
'uploads.*' => 'image|mimes:jpg,jpeg,png'
], $messages);
With this I am getting the error as
The uploads.0 must be an image.
The uploads.0 must be a file of type: jpg, jpeg, png.
I want to have the file name in place of the uploads.0
CodePudding user response:
Hi I think I have got your answer, I found out the same question asked over here https://laracasts.com/discuss/channels/laravel/get-filename-of-failed-multiple-uploaded-files-in-laravel Okay you can do this to your validation
$messages = [];
if($request->hasfile('uploads')){
// dd($request->file('uploads'));
foreach ($request->file('uploads') as $key => $file) {
$messages['uploads.'.$key.'.image'] = 'The ' . $file->getClientOriginalName() . ' must be an image.';
$messages['uploads.' . $key . '.mimes'] = 'The ' . $file->getClientOriginalName() . ' must be a file of type: :values.';
}
}
$request->validate([
// 'customer_name' => 'required|exists:users,id',
'uploads' => 'required|array',
'uploads.*' => 'image|mimes:jpg,jpeg,png'
], $messages);
and for fetching errors into your blade
For getting if no file selected
@if ($errors->has('uploads'))
<div >
<strong>{{ $errors->first('uploads') }}</strong>
</div>
@endif
If multiple selected files have error
@if($errors->has('uploads.*'))
@foreach ($errors->get('uploads.*') as $message)
@foreach ( $message as $value)
<div >{{ $value }}</div>
@endforeach
@endforeach
@endif
Hope it helps
CodePudding user response:
It seem fine but try this it should work.
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();