HTML Upload MAX_FILE_SIZE does not appear to work

HTML Upload MAX_FILE_SIZE does not appear to work

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Client-Side Myth of MAX_FILE_SIZE

A common misconception in web development involves the <input type="hidden" name="MAX_FILE_SIZE"> field. Developers often assume that setting this value provides a hard limit to the user's browser, preventing them from selecting a file larger than the specified byte count before they even attempt to upload it. In reality, this HTML attribute functions strictly as a hint for the PHP server-side script, not as a client-side validation mechanism.

When you set MAX_FILE_SIZE to 2097152 (2MB) in your form, the browser sends that number along with the file data. However, the browser does not inspect this value to stop the user from selecting a 4MB file. The user interface remains completely unaware of the limit until the server processes the request. If you are seeing a successful upload notification or no warning at all when a user uploads a massive file, it is because the client-side validation never occurred. The browser simply transmitted everything and reported back to PHP that the file size was within the "soft" limit indicated by the hidden field.

How PHP Interprets the Limit

From the server's perspective, MAX_FILE_SIZE acts as an upper bound check. When your PHP script receives the POST request, it can retrieve this value using $_POST['MAX_FILE_SIZE']. The standard practice is to compare this against the actual file size available in $_FILES['upload']['size'].

If the actual file size exceeds the value provided in MAX_FILE_SIZE, you should abort the upload immediately. This allows you to handle large files differently—perhaps by rejecting them outright or storing metadata without processing the content. However, relying solely on this check is insufficient for security; you must also respect the directives set in your PHP configuration file (php.ini) and .htaccess rules.

$maxAllowed = isset($_POST['MAX_FILE_SIZE']) ? $_POST['MAX_FILE_SIZE'] : 0;
$uploadedSize = isset($_FILES['upload']['size']) ? $_FILES['upload']['size'] : 0;

if ($uploadedSize > $maxAllowed) {
    die("Error: The file is too large. Maximum allowed size is " . $maxAllowed . " bytes.");
}

// Proceed with moving the file only if the check passes

Why Not Just Check $_FILES['size']?

Your update raises a pertinent question regarding code logic: isn't checking $_FILES['upload']['size'] < 2000 sufficient without the hidden field? The distinction lies in how users interact with different environments.

If you omit the MAX_FILE_SIZE hidden input entirely, your server is left to rely exclusively on php.ini settings like upload_max_filesize. While this provides a robust fallback, it lacks transparency for the user. Without the hidden field, a user attempting to upload a file might hit a generic "File too large" error generated by the web server or PHP configuration without understanding why.

By explicitly sending MAX_FILE_SIZE, you are providing a specific constraint that your application logic can interpret. It allows your backend to differentiate between a file that is simply too big for your system (php.ini limits) and a file that violates your application's specific business rules (e.g., "We only accept files under 2MB regardless of server capacity").

Implementation Best Practices

To implement this correctly, you must ensure the hidden input appears before the <input type="file"> element. Browsers process form controls sequentially; placing MAX_FILE_SIZE first ensures it is parsed correctly in many older browser implementations, although modern browsers are generally lenient with ordering.

Furthermore, always validate against both the MAX_FILE_SIZE hint and your server's configured limits. A file that passes the MAX_FILE_SIZE check might still exceed upload_max_filesize. Ignoring either check creates a vulnerability where oversized files could consume excessive disk space or CPU resources during processing.

For developers using modern frameworks like Laravel, this manual HTML approach is often abstracted away. Laravel's file upload handling and validation rules ($request->file('image')->size() < 2097152) manage these checks internally. However, understanding the underlying mechanics of MAX_FILE_SIZE remains crucial when building raw forms or integrating with non-Laravel backends.

Ultimately, MAX_FILE_SIZE is a cooperative protocol between the form and the server, not a hard stop for the client. It empowers your backend to enforce granular size policies that go beyond the generic limits set in php.ini.

Our network