How to set PHP upload file size limit in PHP
If you’ve got a web application that uploads files to the server through a PHP script, you might have noticed that you can’t upload targe files by default. Depending on how you host your website (or application), there are different ways to change the PHP settings.
The limit is set relatively low by default for stability and security reasons – a malicious script or attacker could attempt to DoS your server with large file uploads and temporarily shut your application down.
The default settings of PHP will restrict the size of any file to be uploaded to 2a maximum of MB. This raises problems for those who want to upload a more excellent file, a 2 MB.
To increase the upload size, you have to change three settings present in the php.ini file:
- post_max_size This is the combined maximum size of all files sent on the form. If you have two file fields on your record, the total file size of the two files must not exceed the post_max_size value.
- upload_max_filesize: This is the file upload limit of each file.
- memory_limit: PHP scripts have a memory limit, which can generally prevent some uploads from working. The limit should be reasonable; you won’t need 20 MB for a simple ‘hello world’ script. Try slowly increasing this value if you find that uploads still aren’t working.
You can set the values by using a numeric value followed by M for the amount of MB you want to allocate for each variable.
If you host your site remotely, you should check their documentation on changing the PHP configuration settings.
Change php.ini directly
If you host your site on a server you can access, you can change your php.ini file directly. This is the most straightforward approach. Your php.ini file should exist in the PHP installation directory. Open it in your favourite text editor, search for these lines, and change them:
memory_limit = 8M
post_max_size = 8M
upload_max_filesize = 2M
Indeed, some applications need the PHP upload_max_filesize to be at least 40 MB. Unfortunately, you cannot have such a high value on the shared server because of possible server overloads due to excessive memory usage.
The upload_max_filesize on our shared servers is set to 24 and cannot be changed. In most cases, this is more than enough, and you will not experience problems because of it. In rare instances where 24 MB is not enough, we recommend using an FTP client to upload the necessary file.
How Does Php.ini Increase the Pload File Size Limit?
The answer to this question can entirely be given, but first of all, we should be familiar with the following terms:
PHP :
PHP originally stood for Personnel Home Page, but now it stands for Hypertext Preprocessor. PHP is an open-source server-side scripting language designed for Web development to produce dynamic Web pages.
Php.ini :
A particular file for PHP and several zests, such as suPHP, is named Php.ini. The Php.ini file is the default configuration file for running applications that require PHP. It is used to control the variables such as upload sizes, file timeouts, and resource limits. The server’s php.ini file is located at /usr/local/lib/php.ini.
File Upload :
File Upload allows the visitors to send files to the web server using standard forms. File Upload was not supported in Internet Explorer 3.0 but was later added in version 3.02. The file upload limit is usually set pretty low by shared hosting providers. Hosting providers do not allow customers to increase the file upload limit.
Php Upload File :
Now, we will discuss two dhp upload files to the server:
If we want to allow users to upload a file to the server, we have to provide a form to specify which file they want to upload. The action page is called once the form’s submit button is clicked. This page needs to contain the PHP code to process the uploaded file.
The Input Form :
Before a user can upload a file, an interface that allows the user to select a file should be provided to them, and thus, the upload is initiated.
The following code is an example of an input form. There are a couple of essential things to note about this code:
- The actions attribute points to a .php file. This is the file that will process the uploaded file.
- There is an attribute called type, whose value is multipart/form-data.
- One of the input fields has type= “file”.
The Action Page :
Once the user uploads a file, the file is uploaded into a temporary directory on the server. If the file cannot be moved, then it will disappear. Therefore, the action page needs to move the file to another location where it can stay as long as required.
Whenever a file is uploaded, you can find specific information about it, including its name, type, size and the name of the temporary file on the server. These details are available via a PHP array called $_FILES.
Therefore, we are well known for the fact that PHP uploads files.
Now, we will discuss how Php.ini Improves the Upload File Size Limit. The Php.ini Increase Upload File Size Limit in the following way:
The server’s PHP setting determines the upload file size. The default values for PHP will not allow us to go beyond the maximum 2MB upload file size. The page for upload modules depends on two PHP settings,i.e.
- post_max_size
- upload_max_filesize
The upload module limits the size of a single attachment to 50% of post_max_size or 100% of upload_max_filesize, whichever is smaller. The default PHP values are 2 MB for upload_max_filesize and 8 MB for post_max_size. These two PHP variables can be changed in several places, most likely php.ini or .htaccess(depending on our hosting situation).
This can further be understood using the following instance:
If we want to increase the limit on uploaded files to 10 MB, then we have to act in the following manner:
Add the following to the relevant php.ini file if accessed. This is a system-wide setting for some hosts. However, if hosts run PHP as a CGI script with exec (for example), these directives can be put in a php.ini file in the root directory.
- upload_max_filesize = 10M;
- post_max_size = 20M;
Add the following to the .htaccess file in the root directory.
- php_value upload_max_filesize 10M
- php_value post_max_size 20M
The Php documentation expounds that the memory_limitsetting also affects file uploading. Naturally, memory_limit should be larger than post_max_size. If such an issue arises, then add this:
- in php.ini, memory_limit = 18M;
- in .htaccess, php_value memory_limit 18M
Hence, how PHP can increase the upload file size limit differs from Increasing it.
Upload File Size Limit As in the above-written section, we are increasing the memory size limit.
