How To Change The Maximum Upload File Size In PHP?

Published October 26, 2024

Problem: PHP Upload Size Limit

Uploading large files through PHP can be difficult due to default file size limits. These limits are set to protect servers from using too many resources, but they can stop you from uploading larger files you need.

Modifying PHP Configuration to Increase Upload Size

Editing php.ini File

To increase the maximum upload file size in PHP, you can edit the php.ini file. First, find the php.ini file on your server. The location varies based on your server setup, but it's often in the PHP installation directory.

Once you've found the php.ini file, open it in a text editor. Look for the upload_max_filesize directive and change its value to your desired maximum file size. For example, to allow uploads up to 40MB, set it to:

upload_max_filesize = 40M

Next, find the post_max_size setting and adjust it to be equal to or greater than the upload_max_filesize:

post_max_size = 40M

After making these changes, save the php.ini file and restart your web server for the new settings to take effect.

Tip: Check PHP Configuration

To check your current PHP configuration settings, create a PHP file with the following code and run it on your server:

<?php
phpinfo();
?>

This will display all PHP settings, including the current upload_max_filesize and post_max_size values.

Using .htaccess File for Upload Size Changes

If you can't access the php.ini file, you can try using an .htaccess file to modify PHP settings. Create or edit an .htaccess file in your website's root directory and add these lines:

php_value upload_max_filesize 40M
php_value post_max_size 40M

This method lets you increase the file size limit without changing the main php.ini file. However, .htaccess files only work on Apache servers with the right module enabled. Some hosting providers may limit the use of .htaccess files for security reasons.

Keep in mind that using .htaccess to change PHP settings may not always override the server's main configuration. If you still have issues after making these changes, you may need to contact your hosting provider for help.

Alternative Methods to Increase PHP Upload Size

Creating Custom php.ini File

If you can't change the main php.ini file, you can create a custom php.ini file for your directory. This method lets you set PHP options for a project or website.

To create a custom php.ini file:

  1. Open a text editor and create a new file named php.ini.
  2. Add these lines to set new upload size limits:
upload_max_filesize = 40M
post_max_size = 40M
  1. Save the file and put it in the root directory of your website or where your PHP scripts are.

This custom php.ini file will override the settings in the main php.ini file for that directory. Some hosting providers may not allow custom php.ini files for security reasons.

Tip: Check PHP Configuration

To verify if your custom php.ini file is being read, create a PHP script with phpinfo() function and look for the "Loaded Configuration File" entry. This will show you which php.ini file is currently in use.

Using ini_set() Function in PHP Scripts

You can also use the ini_set() function in your PHP scripts to change PHP settings. This method changes options at runtime.

To use ini_set() for upload size settings:

  1. Add these lines at the start of your PHP script:
ini_set('upload_max_filesize', '40M');
ini_set('post_max_size', '40M');
  1. Put these lines before any file upload operations in your script.

There are limits to using ini_set():

  • Some settings, like upload_max_filesize and post_max_size, can't be changed at runtime. These are PHP_INI_PERDIR settings, which can only be set in php.ini or .htaccess files.
  • Changes made with ini_set() only apply to the current script and don't affect other scripts or future requests.

Best practices for using ini_set():

  • Use ini_set() for settings that can be changed at runtime, like memory_limit or max_execution_time.
  • Check if the changes were applied using ini_get() after calling ini_set().
  • Use ini_set() as a temporary fix while working on a permanent change through php.ini or .htaccess files.

The most reliable way to increase PHP upload size limits is by changing the main php.ini file or using .htaccess, as discussed in the previous sections.