Laravel mime type Validation Example

Laravel mime type validation example; In this tutorial, we will show you how to validate images or videos using mime-types in laravel. We have added two examples below one for validating photos and another is video laravel validation mime-types. The file under validation must match one of the given MIME types.

You can use these examples in your laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 apps. Let’s see an example of mimetypes validation in laravel below.

Example 1: for Photo or Image

The file under validation must have a MIME type corresponding to one of the listed extensions.

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

Example 2: for Video

$request->validate([
   'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime'
]);

Example 3: for File

$request->validate([
   'file' => 'required|file|mimes:ppt,pptx,doc,docx,pdf,xls,xlsx|max:204800'
]);

To determine the MIME type of the uploaded file, the file’s contents will be read and the framework will attempt to guess the MIME type, which may be different from the client’s provided MIME type.

Leave a Comment