Laravel / PHP PHP Free

PHP: Validate File Uploads Safely

Validate file uploads in PHP by checking the real file type, size and extension — not just what the browser claims.

29 lines · Updated Nov 26, 2026

Preview of PHP: Validate File Uploads Safely
php-secure-file-upload-validation.php
<?php

// Laravel: in a Form Request or controller
$request->validate([
    'photo' => [
        'required',
        'file',
        'image',              // checks the real MIME type
        'mimes:jpg,jpeg,png,webp',
        'max:2048',           // 2 MB
        'dimensions:min_width=200,min_height=200',
    ],
]);

// Plain PHP: check the real type with finfo
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($_FILES['photo']['tmp_name']);
$allowed = ['image/jpeg', 'image/png', 'image/webp'];

if (! in_array($mime, $allowed, true)) {
    die('Only JPG, PNG and WebP images are allowed.');
}

if ($_FILES['photo']['size'] > 2 * 1024 * 1024) {
    die('File is larger than 2 MB.');
}

$path = 'uploads/' . bin2hex(random_bytes(8)) . '.jpg';
move_uploaded_file($_FILES['photo']['tmp_name'], $path);

About this snippet

File uploads are one of the most dangerous features on any website. If an attacker can upload a .php file disguised as an image and then open it in the browser, they can take over your server. This snippet validates uploads properly in PHP: it checks for upload errors, limits the size, detects the real file type from its contents, and stores the file under a safe, random name.

How it works

  1. It rejects uploads that failed or exceed your size limit.
  2. It checks the actual MIME type with PHP's finfo instead of trusting the file extension or the browser-provided type, which attackers can fake.
  3. It allows only an explicit list of types (allow-list), for example JPEG, PNG, WebP and PDF.
  4. It generates a random file name with the correct extension and moves the file with move_uploaded_file().

How to use it

  • Store uploads outside the public web root where possible, and serve them through a script that checks permissions.
  • If they must be public, disable PHP execution in the upload folder.
  • Set matching limits in php.ini (upload_max_filesize, post_max_size) and your web server.

Extra protection

  • Re-encode images (for example with GD or Imagick) to strip hidden payloads and metadata.
  • Never use the original file name in a path — it can contain ../.
  • Scan uploads with an antivirus tool if users share files with each other.

Common problems

  • Large files fail silently: the request exceeded post_max_size, so $_FILES is empty.

FAQ

Laravel? Use validation rules like file|mimes:jpg,png,pdf|max:2048 and store with $request->file('x')->store(), which generates a random name.

Related: OWASP Top 10 explained · Bulk image resizer

How to install

In Laravel, use the validation rules in a Form Request. In plain PHP, use the finfo block before move_uploaded_file. Always store uploads outside the public folder or under a random name.

Share this snippet

See all
Chat with us