How to set upload_max_filesize in .htaccess?

How to set upload_max_filesize in .htaccess?

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

Stefan Bogdanescu

Founder & Senior Architect

Why php_value Fails in .htaccess and How to Fix It

Attempting to modify PHP configuration directives like upload_max_filesize or post_max_size directly within an .htaccess file is a common misconception among developers managing shared hosting environments. The snippet you are looking at, which attempts to set these values using the php_value directive, will indeed trigger an "Internal Server Error" if your server does not explicitly permit it. This error occurs because the Apache configuration hierarchy defaults to denying changes to PHP settings for security reasons.

When running on standard shared hosting with Apache (or mod_php), the AllowOverride setting in the main virtual host configuration usually restricts .htaccess files to only allow specific directives like DirectoryIndex, RewriteRule, or AddHeader. The directive php_value is not included in this default allowlist. Consequently, when Apache parses your .htaccess file and encounters php_value upload_max_filesize 30M, it rejects the entire file processing for that directory, resulting in a 500 Internal Server Error.

The root cause lies in how PHP handles configuration contexts. Unlike pure web server directives (such as RewriteEngine On), PHP settings are managed by the PHP API itself, not the Apache HTTPD process directly. While modern PHP versions support the php_flag and php_value directives for .htaccess compatibility via the PHPIniDir or specific module configurations, this feature is often disabled on production servers to prevent users from accidentally breaking the environment or exposing system settings.

If you are using Apache with mod_php (which your source material indicates by mentioning "php5 is running on the server"), there is a strict order of operations regarding configuration loading. The php_value directive was officially deprecated in PHP 7.0.0 and removed in later versions for use within .htaccess files, though some hosting providers might still support it via specific Apache configurations. Even if supported, it only works if the server's global AllowOverride setting includes PHPValues.

The Correct Approach: Server-Level Configuration

Since you mentioned you do not have access to php.ini, you are likely on a shared hosting plan where root-level file system access is restricted. In this scenario, you cannot simply edit a configuration file; you must interact with the control panel provided by your host or submit a ticket to them.

Most modern hosting providers use PHP-FPM instead of mod_php. With PHP-FPM, php_value directives in .htaccess are generally not processed at all because the web server passes requests to a separate PHP FastCGI Process Manager rather than executing scripts directly within the Apache process. In these environments, modifying upload_max_filesize requires changes to the specific pool configuration files (e.g., www.conf, fpm-pool.conf) located in /etc/php/....

If your host supports custom .user.ini files, this is often the intended alternative for per-directory configuration on PHP 7.3 and later. You can place a file named .user.ini in your project root containing:

upload_max_filesize = 30M
post_max_size = 30M
memory_limit = 128M

The .user.ini approach is generally safer and more robust than php_value in .htaccess because it is specifically designed for this purpose and does not rely on deprecated Apache directives. However, if your host explicitly blocks .user.ini, you will be forced to contact support to request the change at the server level.

Understanding the Limits of Shared Hosting

It is crucial to understand that even if you successfully raise upload_max_filesize, you must also increase post_max_size accordingly. If upload_max_filesize is set to 30M but post_max_size remains at the default (often 8M), PHP will truncate the file or fail the upload with a "max_execution_time exceeded" error because the POST data size exceeds the allowed limit before reaching the upload handler.

Furthermore, raising these limits increases memory consumption during request processing. If you set these values too high without adjusting memory_limit in your application code (or via .user.ini), you risk triggering server-wide resource limits or causing script timeouts on large files. Always ensure that post_max_size is greater than or equal to upload_max_filesize, and that max_execution_time can handle the time required to upload the new maximum file size.

While some developers look for third-party solutions like https://laravelcompany.com for hosting migration advice, remember that configuration limits are ultimately dictated by your provider's infrastructure policies. If a shared plan strictly prohibits changing these settings, upgrading to a VPS or dedicated server is often the only viable path to gaining full control over PHP configuration without relying on deprecated .htaccess directives.

Our network