Increase PHP Upload Size



Apache2 with mod_php

To increase the PHP upload size in Apache 2 with mod_php installed using .htaccess, follow these steps:

  1. Locate or Create .htaccess File: Find the .htaccess file in your website's root directory. If it doesn't exist, create a new text file named .htaccess.

  2. Edit .htaccess File: Open the .htaccess file with a text editor and add the following lines to it:

    php_value upload_max_filesize 64M
    php_value post_max_size 64M
    php_value max_execution_time 300
    php_value max_input_time 300

    This configuration sets the maximum upload size to 64MB and the script execution time to 300 seconds. Adjust these values according to your needs.

  3. Save and Upload: Save the .htaccess file and upload it to your website's root directory if you edited it locally.

  4. Restart Apache: Changes in .htaccess files are typically recognized immediately, but in some cases, you might need to restart Apache for the changes to take effect. You can do this by running the following command in the terminal (requires administrative privileges):

    sudo systemctl restart apache2
  5. Verify Changes: Create a PHP file with the following content to check the updated settings:

    <?php
    phpinfo();
    ?>

    Access this file through your browser. It will display your PHP configuration; search for upload_max_filesize and post_max_size to confirm they reflect your new settings.

Note: Your hosting provider may have restrictions on overriding these values. If the changes do not take effect, contact your hosting support for assistance.



Apache2 through PHP.ini

To adjust PHP settings for version 8.3, particularly for scenarios where you're using PHP-FPM and not mod_php, you will typically need to modify the php.ini configuration file. The process is similar to what has been described, with slight adjustments for PHP 8.3 specific features or changes, if any. Here's a refresher on the steps tailored for PHP 8.3:

  1. Find the php.ini File: Locate your php.ini file where PHP 8.3 configurations are stored. You can determine the location of this file by creating a simple PHP script, e.g., info.php, with the following content:

    <?php
    phpinfo();
    ?>

    Access this script through your web browser to find the path to the php.ini file used by PHP 8.3, listed under "Loaded Configuration File".

  2. Edit the php.ini File: Open the located php.ini file with a text editor. You will need to find and adjust the following directives:

Helpful tip:

To find the exact line number each one of the variables is at use the following command within the commandline (Linux):

cat /etc/php/8.3/fpm/php.ini | grep "upload_max_filesize" -n

865:upload_max_filesize = 2M

cat /etc/php/8.3/fpm/php.ini | grep "post_max_size" -n

713:post_max_size = 8M

As you can see above they are located on lines 865 and 713 respectively. You can view line numbers in nano by using the '-c' directive:

nano -c /etc/php/8.3/fpm/php.ini
  • upload_max_filesize: This sets the maximum size of an uploaded file.
  • post_max_size: This sets the max size of post data allowed. This value should be larger than upload_max_filesize.

Example changes for increasing the file upload size to 64 MB:

   upload_max_filesize = 64M
   post_max_size = 64M

You may also want to adjust max_execution_time and max_input_time to accommodate the upload of large files:

   max_execution_time = 300
   max_input_time = 300
  1. Restart PHP-FPM: After making the necessary changes, save the php.ini file. Restart the PHP-FPM service to apply these changes. The command may vary based on your system and PHP version, but generally, it will be similar to:

    sudo systemctl restart php8.3-fpm

    Ensure you use the correct service name for your PHP 8.3 FPM.

  2. Verify the Changes: Use the same info.php file created in step 1 to verify that your changes have been applied successfully. Access it via your browser and check the values for upload_max_filesize and post_max_size to ensure they reflect your adjustments.

Note: Always backup your configuration files before making changes. If you encounter issues or are unsure about making these changes, especially on a production system, consider consulting with a system administrator. If your server environment is managed by a hosting provider and you don't have direct access to modify the php.ini file, you may need to contact their support team to request these changes.



NGINX

To change the PHP upload size on an NGINX server, you generally need to adjust settings in both the PHP configuration (php.ini) and the NGINX configuration files. This is because both NGINX and PHP have separate settings that limit upload sizes. Here’s how to adjust both:

Adjusting PHP Configuration

  1. Locate the php.ini File: The location of this file can vary depending on your PHP installation. You can find it by creating a PHP file with the following content and accessing it through your browser:

     <?php
     phpinfo();
     ?>

    Look for the “Loaded Configuration File” line to find the path to your php.ini file.

  2. Edit the php.ini File: Open your php.ini file in a text editor. Look for and adjust the following directives:

Helpful tip:

To find the exact line number each one of the variables is at use the following command within the commandline (Linux):

cat /etc/php/8.3/fpm/php.ini | grep "upload_max_filesize" -n

865:upload_max_filesize = 2M

cat /etc/php/8.3/fpm/php.ini | grep "post_max_size" -n

713:post_max_size = 8M

As you can see above they are located on lines 865 and 713 respectively. You can view line numbers in nano by using the '-c' directive:

nano -c /etc/php/8.3/fpm/php.ini
  • upload_max_filesize = 64M: This directive sets the maximum size of an uploaded file.
  • post_max_size = 64M: This directive sets the maximum size of POST data that PHP will accept.

Adjust the 64M to the limit that you require. Ensure post_max_size is greater than or equal to upload_max_filesize.

  1. Restart PHP Processor: After saving changes to the php.ini file, you'll need to restart your PHP processor. The command to restart depends on how PHP is set up on your server. If you're using PHP-FPM, the command might look something like this:

     sudo systemctl restart php8.3-fpm

    Make sure to replace php8.3-fpm with the correct version of PHP-FPM you're using.

Adjusting NGINX Configuration

  1. Edit NGINX Site Configuration: Open your NGINX site configuration file. This file is typically located in /etc/nginx/sites-available/ directory. You might have a default file or a specific file for your site.

  2. Add or Modify Client Max Body Size: In the server block or location block where you want to increase the upload limit, add or modify the following directive:

     client_max_body_size 64M;

    This directive tells NGINX the maximum allowed size of the client request body, effectively setting the limit for file uploads. Adjust 64M to match or exceed the upload limit set in php.ini.

  3. Reload NGINX Configuration: After making changes to the NGINX configuration file, reload NGINX to apply the changes:

     sudo systemctl reload nginx

Verify the Changes

To verify your changes, you can try uploading a file larger than the previous limit but smaller than the new limit you've set. If the upload succeeds without error, your configuration changes have been successfully applied.

Note: It's important to adjust both NGINX and PHP settings to ensure consistency and to prevent upload issues. Also, be mindful of the resources available on your server when increasing upload sizes, as allowing very large uploads can impact server performance and storage.