Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

babai9's avatar

Get filename of failed multiple uploaded files in laravel

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

0 likes
18 replies
babai9's avatar

Hi can anyone reply to my question

babai9's avatar

Hi, can anyone reply to my post No one for reply

Snapey's avatar

@babai9 I can reply. Please format your code if you want your question to be taken seriously.

Sinnbeck's avatar

@babai9 format your code by adding ``` on the line before and after it. You can edit the original post

babai9's avatar

Formatted can you all please reply

Sinnbeck's avatar

@babai9 The problem is that you nest the messages

$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.';
    }

}
$request->validate([   
    // 'customer_name' => 'required|exists:users,id',
    'uploads' => 'required|array',
    'uploads.*' => 'image|mimes:jpg,jpeg,png'
], $messages);
babai9's avatar

@Sinnbeck yes thanks its working, but i also need to have for the mime types how can i do that too

The gadgets.jpg must be an image.

this is just perfect but i need to have the errors showing for mime types too

Sinnbeck's avatar

@babai9 So this?

$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. It has mime ' . $file->getMimeType();
    }

}
$request->validate([   
    // 'customer_name' => 'required|exists:users,id',
    'uploads' => 'required|array',
    'uploads.*' => 'image|mimes:jpg,jpeg,png'
], $messages);
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Or this

$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);
babai9's avatar

@Sinnbeck Okay but now i got this as an error

The gadgets.jpg must be an image. It has mime application/x-empty
The uploads.0 must be a file of type: jpg, jpeg, png.

babai9's avatar

@Sinnbeck Yes thanks it did worked for me, i was stuck for two days, searching for answers almost got me to hell. Thank you maybe a thank you is not enough :)

Sinnbeck's avatar

@babai9 Happy to help. Please mark the answer as best to set the thread as solved :)

babai9's avatar

@Sinnbeck Yes i have checked it and its working fine too, do i need to do something to make this answer correct so that it can help someone

Please or to participate in this conversation.