Laravel File Validation Made Easy: Validate Size, Type, and MIME

When you build any upload form, File Validation In Laravel becomes one of the most important things to get right. You do not want users uploading huge files, wrong formats, or even dangerous content.

In this Laravel file validation tutorial, you will see how to validate file uploads in Laravel step by step. You will learn how to control file type, size, and MIME with clear, practical rules you can drop straight into your project.

Why file validation is important

File validation protects your app, your users, and your server. It helps you:

  • Block unsafe or unexpected file types
  • Prevent very large uploads from filling your storage
  • Stop fake uploads that are not real files
  • Keep image sizes under control for faster pages
  • Enforce business rules like “PDF only” for CVs
  • Improve user experience with clear, friendly error messages

Think about a job portal that accepts CVs, or a store that lets users upload product photos. Without proper File Validation In Laravel, those features can quickly turn into security risks and performance problems.

Core rules for File Validation In Laravel

These rules form the heart of this laravel file validation tutorial. Once you understand them, you will know how to validate file uploads in Laravel in most real projects.

All examples assume a controller method using:

public function store(Request $request)
{
    $data = $request->validate([
        // rules go here
    ]);

    // handle $data
}

file: validate a real uploaded file

Use the file rule to make sure the field contains a real uploaded file, not just a string or fake value. Real-life example: a contact form where users can attach a document.

$data = $request->validate([
    'attachment' => 'required|file',
]);

The above request validation the attachment from request should be file and it’s required to process this request.

image: ensure the file is an image

The image rule checks that the uploaded file is one of these types: jpg, jpeg, png, bmp, gif, svg, or webp. Use this for profile pictures, product images, and logos.

$data = $request->validate([
    'avatar' => 'required|image',
]);

mimes: control allowed extensions

Sometimes you want to allow only specific extensions, for example only jpg and png for a blog cover image or assuming for video mov or mp4.

$data = $request->validate([
    'cover' => 'file|image|mimes:jpg,jpeg,png',
    'video' => 'file|mimes:mp4,mov,avi',
    'pdf'   => 'file|mimes:pdf',
]);

In this example, we have validated different types of values like image, video and pdf.

mimetypes: validate actual MIME type

Extensions can be faked, but the mimetypes rule checks the actual MIME type reported by the file. For example, only allow MP4 videos for a short promo upload:

$data = $request->validate([
    'promo_video' => 'required|file|mimetypes:video/mp4',
]);

This gives you stricter control than mimes. It is very useful when you host user-generated videos or audio and must keep formats consistent.

Validating size of file

The size validation can be helpful when you want to set some quality standards. like what will be minimal and maximum allowed size as per uploaded files.

The min rule can apply to files, and for files it uses kilobytes (KB). This is great when you want to avoid tiny, empty, or corrupted uploads. The max rule is one of the most common parts of any laravel file size and mime type validation guide. Again, for files it uses kilobytes.Instead of combining min and max, you can use between to set both in one rule. Again, the values are in kilobytes for files.

$data = $request->validate([
    'avatar' => 'required|image|min:2048',
    'cv' => 'required|file|mimes:pdf|max:5120',
    'gallery_image' => 'required|image|between:50,5120',
]);

Here, it is validating size for different type of inputs like avatar, cv and images.

dimensions: control width, height, and ratio

The dimensions rule lets you control the pixel size of images. This is powerful when you want consistent layouts. Let’s take an example to allow user to upload square avatar between 200×200 and 600×600 pixels:

$data = $request->validate([
    'avatar' => 'required|image|dimensions:min_width=200,min_height=200,max_width=600,max_height=600',
]);

Another example can be used for ratios.

$data = $request->validate([
    'avatar' => 'required|image|dimensions:min_width=200,min_height=200,max_width=600,max_height=600',
]);

Conclusion

File validation in Laravel ought should be considerably less enigmatic by now. You’ve seen how basic rules like file, image, mimes, mimetypes, min, max, between, and dimensions may enhance user experience and safeguard your application. Every time you introduce a new upload functionality, use this as your go-to guide for validating Laravel files. Type rules should come first, followed by size restrictions, dimensions, and MIME tests.

With these tools, you can confidently verify file uploads in Laravel, maintain minimal storage, and provide users with unambiguous notifications when something goes wrong.