How to Increase File Upload Size in PHP Using .htaccess
Why Increase File Upload Size?
Key PHP Directives that Impact File Uploads
- upload_max_filesize: This sets the maximum size of an uploaded file.
- post_max_size: This limits the size of the entire request body, including all form fields.
- memory_limit: This defines the maximum amount of memory a script is allowed to use.
Using .htaccess to Increase PHP File Upload Size
The easiest and most common way to increase the file upload size limit in PHP is by using the .htaccess file. This method is particularly useful for those who do not have access to the main PHP configuration (php.ini), which is often the case on shared hosting environments.
Follow these steps:
The .htaccess
file is a configuration file used by web servers running Apache. It allows you to override some server configurations, such as file upload limits, on a per-directory basis.
- If you’re running a website, this file is often located in the root directory of your application or site (e.g.,
/public_html
or/www
). - If you don’t already have a
.htaccess
file, you can create one using a plain text editor and save it as.htaccess
.
Step 2: Edit the .htaccess File
Open your .htaccess
file in a text editor and add the following lines to increase the PHP file upload size:
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value memory_limit 128M
php_value max_execution_time 300
php_value max_input_time 300
Explanation of the Directives:
- upload_max_filesize 64M: This sets the maximum allowed size for uploaded files to 64MB. You can adjust this value based on your needs.
- post_max_size 64M: This defines the maximum size of POST data, which includes file uploads. Make sure this is equal to or larger than
upload_max_filesize
. - memory_limit 128M: This controls how much memory your PHP scripts are allowed to use. It’s a good idea to increase this if you’re handling large file uploads.
- max_execution_time 300: This sets the maximum time, in seconds, that a PHP script can run. Large files take more time to upload, so increasing this is essential.
- max_input_time 300: This controls the time PHP scripts are allowed to parse input data. Set this high enough to ensure large files are processed.
Step 3: Save and Upload the .htaccess File
After making the changes, save the file and upload it back to your server using an FTP client or your hosting provider’s file manager.
Step 4: Test the Configuration
To ensure the changes have been applied, try uploading a file larger than the previous limit. If everything works correctly, the file should upload without any errors.
You can also check the updated settings by creating a phpinfo()
file:.
- Create a file called
info.php
and add the following code:
Additional Tips
- Adjust the limits based on your needs: You don’t need to go overboard with the limits. Set the values based on the largest file you expect users to upload. For example, if the maximum file size is 20MB, you can set upload_max_filesize to 20M and post_max_size to something slightly larger like 24M.
- Consult your hosting provider: Some shared hosting providers may restrict the ability to modify PHP settings via
.htaccess
. If you still face issues after making these changes, reach out to your hosting provider for guidance or to request higher limits.