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 by default you can’t upload terribly large files. Depending on how you host your website (or application) there are different ways to change the PHP settings.
The reason the limit is set fairly low by default is 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 2 MB maximum. This arise a lot of problems for those who want to upload a file which is greater than 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 2 file fields on your form, the total file size of the 2 files must not exceed the post_max_size value.
- upload_max_filesize: This is the file upload limit of each individual file.
- memory_limit: PHP scripts have a memory limit, and generally speaking this can prevent some uploads from working. The limit should be set at a reasonable level, of course you won’t need 20mb 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 how to change the PHP configuration settings.
Change php.ini directly
If you host your site on a server that you have access to, you can change your php.ini file directly. This is the easiest approach. Your php.ini file should exist in the PHP installation directory. Open it in your favourite text editor and 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 24Mb and it cannot be changed. In most cases this is more than enough and you will not experience problems because of it. In rare cases where 24Mb are not enough, we recommend using an FTP client to upload the necessary file.
How does Php.ini Increase Upload File Size Limit?
The answer to this question can fully be given but first of all we should be familiar with the following terms:
PHP originally stood for Personnel Home Page but now it is stand for Hypertext Preprocessor. PHP is an open-source server-side scripting language designed for Web development to produce dynamic Web pages.
A special file for PHP and several zests such as suPHP is named as 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 soon afterwards added in version 3.02. The file upload limit is usually set pretty low by shared hosting providers. All hosting providers does not allow their customers to increase the file upload limit.
Php Upload File :
Now we will discuss that how does Php upload file to the server:
If we want to allow users to upload a file to the server, then we have to provide a form for them to specify which file they want to upload. Once the submit button of the form is being clicked, the action page is called. This is the page that 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 being initiated.
The following code is an example of an input form. There are a couple of important 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 enctype, and its 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 will not 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 it is required.
Whenever a file is uploaded, you can find out certain information about the file including its name, type, size, as well as the name of the temporary file on the server. These details are made available to you via a PHP array called $_FILES.
Therefore, we are well known with fact that how PHP upload file.
Now we will discuss about the fact that how does Php.ini Increase Upload File Size Limit. The Php.ini Increase Upload File Size Limit in a 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 setting i.e.
The upload module limits the size of a single attachment to be 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 a number of places by mean of most likely being php.ini or .htaccess(depending on our hosting situation).
This can further be understood by mean of the following instance:
If we want to increase the limit on uploaded files to 10 MB then we have to act in a following manner:
Add the following to the relevant php.ini file, if it can be accessed. This is a system-wide setting for some hosts. However, if hosts are running PHP as a CGI script with suexec (for example) then 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. It is but natural that 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 can PHP increase upload file size limit is different from the fact that how can Php.ini Increase
Upload File size Limit as in the above written section we are increasing memory size limit.