Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Validating Uploaded File Sizes and Image Dimensions in Laravel

When you write a Laravel endpoint that accepts an image, validate the upload’s size, file type, and pixel dimensions before storing it. These checks answer three questions: how many bytes the file contains, whether Laravel recognizes it as an image, and whether its width and height meet your requirements.

PHP receives the request and puts the uploaded file in temporary storage. Laravel then checks the file before your code saves it to the filesystem you configured for file storage. The example below targets Laravel 13 and shows how to validate an image before storing it.

Validate size, type, and dimensions together

Use Laravel’s fluent File rule to check image type and file size on one uploaded field:

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;

public function store(Request $request)
{
    $request->validate([
        'photo' => [
            'required',
            File::image()
                ->min('1kb')
                ->max('10mb'),
        ],
    ]);

    $path = $request->file('photo')->store('photos');

    return $path;
}

Here, File::image() checks that Laravel recognizes the uploaded file as an image. The size rules set a minimum of 1 KB and a maximum of 10 MB. Add Rule::dimensions() constraints to check pixel measurements. You can set limits for width and height or require a particular ratio. Choose limits that fit the images your endpoint needs.

Laravel’s image and file validation rules describe the File builder, which lets you chain validation rules, and the Rule::dimensions() API. Dimension constraints check pixel measurements. You can set minimum or maximum width and height, require exact dimensions, or specify an aspect ratio.

What each rule checks

The size rule limits the file’s bytes. In Laravel’s fluent syntax, you can write readable units such as 1kb and 10mb. When you use a string rule such as max, Laravel measures uploaded-file sizes in kilobytes. Set the limit in kilobytes to match your application’s needs. A size limit does not establish that a file is an image.

The image rule checks the uploaded file’s recognized type. Laravel 13’s documented image formats are JPG, JPEG, PNG, BMP, GIF, WebP, AVIF, HEIC, and HEIF. SVG files are excluded by default; Laravel lets you opt in with File::image(allowSvg: true). Enable SVG only when your endpoint intends to accept it.

A filename extension and a file’s recognized type are different signals. Laravel’s File::types([...]) rule uses the supplied extensions to determine which MIME types, or media types, to accept based on the file’s contents. The separate extensions rule checks the extension in the submitted filename. If your requirements include both the content type and the filename extension, validate both deliberately; Laravel cautions against relying on the filename extension alone.

Validation runs before filesystem storage

PHP receives the multipart request, which carries uploaded files, and places the file in a temporary directory. Laravel makes the file available through the request and checks it against the configured rules. If $request->validate() finds an error, Laravel returns validation errors and does not continue to the store() call that follows. Laravel describes retrieving request files and storing them as separate steps in its HTTP request documentation.

Laravel checks the file before your code stores it on the configured filesystem. This does not stop the client from sending the file over the network or PHP from receiving it first. The PHP settings upload_max_filesize and post_max_size set upload limits that apply before Laravel checks the file. PHP requires post_max_size to be larger than upload_max_filesize. Check these settings along with Laravel’s rules, because Laravel’s size limit does not replace PHP’s upload limits.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *