Your Trusted Web Hosting Partner Since 2018

Increase File Upload Size in PHP using .htaccess

How to Increase File Upload Size in PHP Using .htaccess

If you’re developing a PHP-based website and you require to upload bigger files, you may probably face a problem: the default PHP file upload size limit. By default, most PHP installations have limits set that might not be sufficient for uploading larger files such as videos, images, or even PDFs. These limits can be the root cause of the error which, in turn, can result in a situation where both users and developers are being affected.
This blog will be your puppy and this walk the way to increase file upload size in PHP using .htaccess. Not only will we explain the phenomenon of why a file is too big to upload but also how to fix it with the touch of a finger.

Why Increase File Upload Size?

PHP in its default option of upload size limits a file that is too big to make the server not be subjected to the drastic action of the big files. Out-of-the-box, the PHP file upload size limit is set at 2MB. However, for many modern websites, especially those with multimedia, this limit is still very tight and they probably think they have a temporary breakaway.
If you are implementing a CMS like WordPress or a web application or having an e-commerce store issues can arise while trying to increase the size.

Key PHP Directives that Impact File Uploads

Before we dive into how to change these limits via .htaccess, it’s important to understand the key PHP settings that control 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.
For smooth operation, you need to adjust these settings to accommodate larger file uploads.

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:.

  1. Create a file called info.php and add the following code:
				
					<?php
phpinfo();
?>

				
			

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.

Subscribe to our Resources

Get latest industry news and updates

Popular Articles

Scroll to Top