$_FILE upload large file gives error 1 even though upload_max_size is bigger than the file size

$_FILE upload large file gives error 1 even though upload_max_size is bigger than the file size

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

Stefan Bogdanescu

Founder & Senior Architect

$_FILE upload large file gives error 1 even though upload_max_size is bigger than the file size

When debugging a PHP upload failure, it is tempting to assume that if upload_max_filesize and post_max_size are configured correctly in your php.ini, the server will accept any file within those limits. However, relying solely on these directives often leads to confusing behavior where a 5MB file triggers an UPLOAD_ERR_INI_SIZE error despite the configuration allowing it.

The root cause of this discrepancy lies in how PHP parses the $_FILES superglobal array before applying the size checks defined in php.ini.

When a form uses enctype="multipart/form-data", the browser sends the file content along with metadata, including a hidden field named MAX_FILE_SIZE. If you define this field in your HTML:

<input type="hidden" name="MAX_FILE_SIZE" value="5900000">

and set your PHP configuration to upload_max_filesize = 7MB and post_max_size = 8MB, you might expect a 5MB file (approximately 5,242,880 bytes) to upload successfully. Instead, the server returns error code 1 (UPLOAD_ERR_INI_SIZE).

This happens because PHP performs a client-side sanity check using the MAX_FILE_SIZE value before it even reads the configuration directives from php.ini. Specifically, if the MAX_FILE_SIZE field is present in the request, PHP compares the actual file size against this value. If the file exceeds the value provided in MAX_FILE_SIZE, PHP immediately flags it as an error, ignoring the upload_max_filesize setting entirely for that specific check.

In your specific scenario, you are uploading a 5MB file but telling PHP the limit is 5,900,000 bytes (approx. 5.6MB). While this should theoretically work if the file is strictly under 5.9MB, there are two critical factors causing the failure:

  1. Inclusive vs. Exclusive Logic: The MAX_FILE_SIZE check is strict. If your file size calculation or browser reporting differs by even a few hundred bytes, it might exceed 5,900,000 slightly. However, the most common reason for error 1 in this specific setup is that the MAX_FILE_SIZE directive acts as a hard cap that overrides the php.ini settings when present.
  2. The Real Culprit: If the file size reported by the browser is actually larger than 5,900,000 bytes, PHP throws error 1 immediately. But if the file is genuinely smaller and you are still seeing this error, it suggests a mismatch in how the size is being interpreted or that another directive is interfering.

The most reliable way to resolve this inconsistency is to either remove the MAX_FILE_SIZE input from your form or ensure its value is significantly higher than both upload_max_filesize and post_max_size. When MAX_FILE_SIZE is absent, PHP defaults to checking against the upload_max_filesize directive in php.ini, which aligns with your configuration of 7MB.

Implementation Best Practices

To avoid this class of issues in production environments, follow these guidelines:

  • Align Client and Server Limits: Always ensure that the MAX_FILE_SIZE value matches or exceeds the post_max_size setting in php.ini. If you set post_max_size = 8MB, your hidden input should reflect a value close to that limit (e.g., 9000000).
  • Remove Redundant Checks: Unless you have a specific business logic requirement to reject files immediately on the client side based on this value, it is safer to rely on upload_max_filesize and post_max_size. These are standard, well-documented directives that behave predictably across different server configurations.
  • Verify Actual File Sizes: Browser-reported file sizes can sometimes differ from actual byte counts due to compression or encoding nuances. Always verify the uploaded file size on the server side using filesize($file['tmp_name']) in your PHP code before processing it further.

Troubleshooting Steps

If you encounter this error again, inspect the following:

  1. Check post_max_size: Ensure that MAX_FILE_SIZE does not exceed post_max_size. If post_max_size is 8MB and MAX_FILE_SIZE is set to 5.9MB, a valid file could still trigger errors if the parsing logic interprets the boundary strictly.
  2. Inspect php.ini: Confirm that upload_max_filesize and memory_limit are set as expected. Note that memory_limit affects how much memory PHP can use during processing, not just the upload size, but it is good practice to keep it reasonable (e.g., 64MB) to prevent out-of-memory errors on large uploads.
  3. Use Modern Frameworks: When using Laravel or similar frameworks, these issues are often abstracted away because they rely heavily on php.ini directives rather than manual form field manipulation. If you are building a robust file upload system, consider leveraging existing packages that handle validation and storage securely without needing to manually manage MAX_FILE_SIZE.

For developers looking to implement secure file uploads with strict validation rules, checking out resources from https://laravelcompany.com can provide insights into handling file uploads within modern PHP applications.

Our network