Tutorial on Correctly Setting Allowed Upload File Size in PHP

In PHP, the size of the uploaded file can be freely set, but after reading some online tutorials, many newbies find that after the modified upload_max_filesizevalue, uploading a larger file will cause the upload to fail. This tutorial will detail how to properly set the allowed upload file size in PHP.

Set via php.ini

First, you need to find the path where the php.ini file is located. If you are not sure, you can check it through PHPinfo.

For example, create a phpinfo.php file with the following content:

<?php
phpinfo();
?>

Upload the file to the host root directory or subdirectory, and then access the file URL in the browser, example: https://www.bkzzz.com/phpinfo.php, you can see something similar to the following, and Loaded Configuration Filethe found value is as follows Example:

View php.ini path through phpinfo

Look in the php.ini file for:

upload_max_filesize
post_max_size
memory_limit
max_execution_time

upload_max_filesize is the upload attachment size, modify this parameter, for example to: upload_max_filesize = 128M.

post_max_size is the post size, and the set value must be greater than or equal to upload_max_filesize.

memory_limit is the execution memory, generally set to 64M or 128M. If upload_max_filesize and upload_max_filesize are set too large, and the value of memory_limit is set too small, it may cause failure when uploading large files.

max_execution_time Execution timeout, in seconds. Modifying it to 0 means unlimited. This value will also affect the uploaded file. If the uploaded file is large, the value of max_execution_time should be increased accordingly.

Recommended settings:

upload_max_filesize = 128M 
post_max_size = 128M 
memory_limit = 256M
max_execution_time =300

Note: If the server has less memory, the value of memory_limit can be changed to 64M-128M.

After the setting is completed, you need to restart PHP to take effect. You can check whether the set value takes effect through phpinfo.

Pagoda panel user settings

If you are a pagoda panel user, you can directly modify the above content in the PHP settings of the pagoda panel, for example:

Setting PHP parameters in the pagoda panel

Leave a Comment