How to validate a file type in laravel

2019-06-28 03:57发布

问题:

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'
    ];
} 

回答1:

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



标签: laravel-5