ini_set("upload_max_filesize","200M") not working in php

ini_set("upload_max_filesize","200M") not working in php

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

Stefan Bogdanescu

Founder & Senior Architect

Why ini_set("upload_max_filesize", "200M") Fails in PHP

You are attempting to override the default file upload limit using a runtime configuration call, but your script is reporting that the value remains stuck at 2M. The root cause isn't a bug in your logic; it is a fundamental restriction within the PHP interpreter regarding which directives can be modified after the server has started.

The directive upload_max_filesize is classified as a PHP Core configuration option. More critically, it is marked as Unsettable at runtime. When you execute ini_set(), PHP checks the internal flags associated with that specific directive. If the flag indicates "User Settable" (or similar variants allowing modification), the change takes effect immediately. For core directives like upload_max_filesize, post_max_size, and memory_limit (in some contexts), this flag is strictly disabled to prevent application-level code from accidentally overriding critical resource constraints set by the system administrator.

Even if you were using a user-space directive or one that allowed runtime changes, the output showing "2M" suggests your environment might be enforcing a stricter limit elsewhere or the specific directive simply refuses to accept the assignment. The first echo in your snippet reads the value from the active configuration stack (which reflects the compiled php.ini or loaded .htaccess rules). The second call to ini_set() attempts to modify it, but because the internal flag for this core directive is set to "No", the function returns false silently. Consequently, the third echo reads the original value again.

To actually increase the upload limit beyond what is defined in your configuration files, you must address the source of truth: the configuration stack itself. The order of precedence is strict and non-negotiable.

  1. Compiled Configuration: This is hardcoded into the PHP binary at build time. If upload_max_filesize was set here to 2M, it cannot be changed via php.ini or ini_set().
  2. php.ini Files: Located in the installation root, user directories (for CLI scripts), or specified via command line. This is the standard place for production adjustments.
  3. Environment Variables: Modern PHP installations often allow overriding settings via environment variables before the SAPI initializes the configuration.
  4. .htaccess / php.ini.user / Virtual Host Configs: These apply only if your web server (Apache/Nginx) is configured to read them and if the directive is marked as PHP_INI_USER or PHP_INI_ALL.

If you are running this code in a standard Apache environment, simply adding .htaccess entries won't work for core directives. You must edit the main php.ini file or the specific virtual host configuration where PHP is loaded. Look for the line:

upload_max_filesize = 2M
post_max_size = 2M

Note that post_max_size must always be larger than upload_max_filesize. If you set the upload limit to 300M but leave post_max_size at 8M, PHP will truncate the request or fail validation because the post body exceeds the allowed size.

If you are deploying on a platform like Laravel Forge or using Docker, ensure your environment variables match these requirements. For example, setting PHP_UPLOAD_MAX_FILESIZE=300M before starting the container might take precedence over the file configuration.

There is also a hidden ceiling imposed by memory limits. Increasing the upload size without adjusting memory_limit can cause fatal errors during processing if the script tries to hold large data in memory simultaneously. Ensure memory_limit is sufficient for your specific use case, though it does not directly cap file uploads.

For complex deployment scenarios or when you need a centralized way to manage these settings across multiple environments without touching server files, tools like LaravelCompany offer infrastructure solutions that handle environment-specific configuration overrides efficiently, ensuring your application behaves consistently regardless of the underlying server setup. However, for most standard deployments, editing the php.ini file remains the most reliable method to enforce a persistent change that survives server restarts and cannot be accidentally reset by runtime code.

Our network