How to change the maximum upload file size in PHP: a complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why doesn't PHP allow uploading files larger than 2 MB?

\n

Have you ever tried to upload a high-quality photo, presentation, or video to a website, and PHP responded dryly: «The uploaded file exceeds the upload_max_filesize directive in php.ini»? Sound familiar? This is a classic problem encountered in nearly every second project. On Stack Overflow, the question «Change the maximum upload file size» has garnered tens of thousands of views — and for good reason.

\n

In this article, we will break down how file upload limits work in PHP, where they are configured, and how to properly increase the limit. You will learn not only about upload_max_filesize, but also about the pitfalls that can render all your efforts useless.

\n\n

How does PHP limit file uploads?

\n

When you send a file via a form, PHP checks its size at several levels. If even one of them rejects it, the file will not be uploaded. Let's look at the three main parameters you need to change to make change the maximum upload file size work correctly.

\n\n

1. upload_max_filesize — the direct file limit

\n

This is the most obvious parameter. It sets the maximum size for a single uploaded file. By default, PHP has 2M (2 megabytes). To increase it, for example, to 100 MB, you need to write:

\n
upload_max_filesize = 100M
\n\n

2. post_max_size — the size of the entire POST request

\n

This is where many people get caught. Even if you increased upload_max_filesize but forgot about post_max_size, the upload will still fail. Why? Because the file is transmitted inside the POST request, and if the total request size exceeds the limit, PHP will simply truncate it.

\n

The rule is simple: post_max_size must be larger than upload_max_filesize. Usually, it is set 20-30% higher. Example:

\n
post_max_size = 120M
\n\n

3. max_execution_time — time for the upload

\n

Large files don't upload instantly. If the file weighs 100 MB and the internet is slow, the script may simply not finish within the allotted time. By default, max_execution_time = 30 seconds. For large files, increase it to 300-600 seconds:

\n
max_execution_time = 600
\n\n

4. memory_limit — memory for processing

\n

Some PHP scripts load the file into memory before processing. If memory_limit is smaller than the file size, you will get an error. It is recommended to set it larger than post_max_size:

\n
memory_limit = 256M
\n\n

Where and how to change the settings?

\n

Now that we know which parameters are responsible for change the maximum upload file size, let's look at where to change them. You have three main options:

\n\n

Method 1: php.ini (main configuration file)

\n

This is the most reliable method. Find the php.ini file on the server (usually located in /etc/php/ or /usr/local/etc/). Open it and find the required directives. If they are not there, add them at the end of the file:

\n
; Allow file uploads (should be On)\nfile_uploads = On\n\n; Maximum size of a single file\nupload_max_filesize = 100M\n\n; Maximum size of the POST request\npost_max_size = 120M\n\n; Script execution time\nmax_execution_time = 600\n\n; Memory limit\nmemory_limit = 256M
\n

After making changes, be sure to restart the web server:

\n
sudo service apache2 restart   # for Apache\nsudo service nginx restart     # for Nginx
\n\n

Method 2: .htaccess (Apache only)

\n

If you don't have access to php.ini (for example, on shared hosting), you can try using .htaccess in the site root:

\n
php_value upload_max_filesize 100M\nphp_value post_max_size 120M\nphp_value max_execution_time 600\nphp_value memory_limit 256M
\n

Important: this method only works

Blogs

Book Recommendations