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

Neeraj1005's avatar

File(docx,pdf) and image(jpeg,png etc..) support validation laravel

In my form I have a file upload tag....but I want to both type of file upload support such as user can upload pdf,docx,image aswell. Can anyone tell me what validation I have to make...?

This is my validation

$validatedData = $this->validate([
            'product_name' => 'required',
            'images.*' => 'image|max:1024',
        ]);
0 likes
6 replies
Snapey's avatar

specify the mime types you want to allow

1 like
Neeraj1005's avatar

@snapey should I have to remove image?

'images.*' => 'mimes:jpg,bmp,png,pdf,docx|max:1024'

like this => it is correct way..?

mvd's avatar

Yes, remove the image rule.

1 like
Snapey's avatar

yes, its the right way. You might want to rename the field if it is not just images

1 like
mvd's avatar
mvd
Best Answer
Level 48

Hi @neeraj1005

Image

The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).

https://laravel.com/docs/8.x/validation#rule-image

You can use a MIME Rule and add the validation mime types from the image validation.

$validatedData = $this->validate([
            'product_name' => 'required',
            'images.*' => 'mimes:jpg,jpeg,png,bmp,gif,svg,webp,pdf,docx|max:1024',
        ]);

https://laravel.com/docs/8.x/validation#basic-usage-of-mime-rule

1 like
rapa's avatar

For Laravel 7+ to validate doc, docx you need to create mimes.php in config directory and add the following content,

config/mimes.php

<?php
 
return [
    'doc'  => array('application/msword', 'application/vnd.ms-office'),
    'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
];
1 like

Please or to participate in this conversation.