Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a file type in laravel

Tags:

laravel-5

I need to validate that a user only uploads a .jpg

I have the following request class. I made the image required but don't know how to check that it is only .jpg

public function rules()
{
    return [
        'name' => 'required|min:3',
        'sku' => 'required|min:5|unique:products',
        'description' => 'required|min:20',
        'price' => 'required',
        'image' => 'required'
    ];
} 
like image 606
master Avatar asked Aug 24 '15 09:08

master


2 Answers

Does it have to just be .jpg? If so then something like:

'image' => 'required|mimes:jpeg'

Documentation: http://laravel.com/docs/5.1/validation#rule-mimes

If it can be any type of image, then:

'image' => 'required|image'

Documentation: http://laravel.com/docs/5.1/validation#rule-image

like image 60
Thomas Jensen Avatar answered Oct 03 '22 17:10

Thomas Jensen


laravel 8

\request ()->validate(['photo' => 'mimes:jpg,bmp,png']);

official documentation link

like image 4
saad Avatar answered Oct 03 '22 18:10

saad