Maximum Upload Size in PHP

Shraddha Paghdar Jan 30, 2023
PHP
  1. php.ini in PHP
  2. .htaccess in PHP
  3. upload_max_filesize in PHP
  4. Change Maximum Upload Size Using upload_max_filesize in PHP
  5. Change Maximum Upload Size Using post_max_size in PHP
Maximum Upload Size in PHP

Uploading files is the transmission of file data from a client like a browser to a server. Nowadays, people don’t store larger files on their computers or mobile phones. They usually store all these files in the cloud or a larger computer system. In today’s post, we will learn how to set & increase the upload size limit in PHP.

Before understanding this, let’s understand what is php.ini & .htaccess in PHP.

php.ini in PHP

PHP contains a configuration file that is read by the server every time it is initialized. Every time PHP is installed, this file is created by default with the default configuration settings. For example, settings related to resource limits, upload maximum size, display log errors, the maximum time to execute a PHP script, etc., are defined in this file. Server admin can change the variable values to declare the changes. All the global or default configurations are defined in this file.

.htaccess in PHP

.htaccess stands for hypertext access which is also a configuration file provided by PHP. This file is used to make changes in the configuration on a directory basis.

The main difference between both these files is that php.ini is used for the whole server while the .htaccess file is only used when a request goes to the directory or subdirectory the .htaccess file is located in. So users can specify specific configurations for individual directories in .htaccess files. Certain web hosting providers sometimes do not allow access to the php.ini or server configuration but only allow .htaccess files.

upload_max_filesize in PHP

Each server has its own capacity to allocate resources. Hosts can set this limit in a server file called php.ini, which indicates how much file data the server can process without affecting the server’s performance. If a user tries to upload media larger than server capacity, it will throw an error the uploaded file exceeds the upload_max_filesize directive in php.ini. The default value is 2M. You can define size in K (kilobyte), M (megabyte), and G (gigabyte).

Change Maximum Upload Size Using upload_max_filesize in PHP

  • Go to /etc/php/7.4/apache2 in Linux and WAMP icon->PHP in Windows.
  • Open php.ini file.
  • Locate the variable upload_max_filesize and update the value based on need.
  • Close the file & restart the server.

Change Maximum Upload Size Using post_max_size in PHP

Each request sent to the server contains much information along with media data. This variable sets the limit of the entire request body, which could include many files. post_max_size must be greater than or equal to upload_max_filesize and less than or equal to memory_limit. If it’s less than upload_max_filesize, it will only process data of the post_max_size limit. The default value is 8M. You can define size in K (kilobyte), M (megabyte), and G (gigabyte).

  • Go to /etc/php/7.4/apache2 in Linux and WAMP icon->PHP in Windows.
  • Open php.ini file.
  • Locate the variable post_max_size and update the value based on need. Make sure it’s more than upload_max_filesize.
  • Close the file & restart the server.
Shraddha Paghdar avatar Shraddha Paghdar avatar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn