Technology

How to increase PHP memory limit and load file limit and script execution time

A PHP memory limit of 16MB is set by default in PHP. Some sites may need more than 32MB. Memory limits of 64MB and above are not unusual. The default upload file limit is 2MB, it’s pretty low, especially if the site works with photos that can easily be more than 32MB in size. As for the script execution time, it is set to 30 seconds by default, but some tasks (for example, service cron tasks) need more time to finish their work. There are several techniques to increase PHP’s memory limit, file upload limit, and execution time; you only need to use one of them. The one that is right for you depends on your system configuration.

1. Set memory limit

1.1. Setting up php.ini This is the recommended approach if you have access to the php.ini from the server. This will not be possible in most shared hosting environments, although your host may be able to adjust it for you. Please note that this change will affect all PHP scripts and websites on the server. – Locate the php.ini file used by your web server. You can use the PHP phpinfo () function to find it. – Edit the memory_limit parameter in the php.ini file (usually in a section called Resource Limits) memory_limit = 32M; Maximum amount of memory a script can consume (32 MB) If there is no section for this, put the previous line at the end of the file. – Restart Apache. Note: If you are using XAMPP / WAMP, there may be two PHP.ini files (one in the PHP directory and the other in Apache / bin). To change your memory limit, edit the file in the XAMPP / Apache / bin directory. The next two solutions are more restricted in scope and, in some cases, may be more appropriate options that affect all sites.

1.2..Htaccess configuration This is useful when you do not have access to the php.ini file. Edit (create) the .htaccess file in the root (public) directory of the site and add the following line: php_value memory_limit 32M This method will only work if PHP is running as an Apache module.

1.3. Configuration in PHP scripts Add the following line where you need to give PHP more memory: ini_set (‘memory_limit’, ’32M’);

1.4. Memory limits in shared enclosures. In some shared hosting environments, access to the PHP memory limit settings is restricted. If you can’t make the change yourself, ask your hosting provider to adjust it for you, or find a new server that allows you more flexibility.

2. Set upload file size limit Your php installation puts limits on upload file size. The default will restrict you to a maximum upload file size of 2MB. You must set the following three configuration options: – upload_max_filesize: the maximum size of an uploaded file. – memory_limit: sets the maximum amount of memory in bytes that a script can allocate. This helps prevent poorly written scripts from consuming all available memory on a server. Note that to have no memory limit, set this directive to -1. – post_max_size: sets the maximum size of the post data allowed. This setting also affects file uploads. To upload large files, this value must be greater than upload_max_filesize. If memory limit is enabled by your configuration script, memory_limit also affects file upload. Generally speaking, memory_limit should be greater than post_max_size.

There are two methods to solve this problem.

2.1. Php.ini Settings – Find the php.ini file used by your web server. You can use the PHP phpinfo () function to find it. – find and modify the following parameters: memory_limit = 32M upload_max_filesize = 10M post_max_size = 20M – restart Apache

2.2. .Htaccess configuration This is useful when you do not have access to the php.ini file. Edit (create) the .htaccess file in the root (public) directory of the site and add the following line: php_value upload_max_filesize 10M php_value post_max_size 20M php_value memory_limit 32M This method will only work if PHP is running as an Apache module.

3. Increase script execution time PHP scripts can only be executed for a certain period of time, and when it reaches the time limit, it will stop and produce the following error: “Fatal error: maximum execution time of 30 seconds in yourscript.php “To allow your PHP script to run longer, you will need to increase the maximum execution time limit for PHP scripts using any of the following methods.

3.1. Php.ini Settings – Find the php.ini file used by your web server. You can use the PHP phpinfo () function to find it. – find and modify the following parameter: max_execution_time = 45 Change the value (in seconds) or set it to 0 for infinite time – restart Apache

3.2..Htaccess configuration Edit (create) the .htaccess file in the site root directory (public) or in your script directory and add the following line: php_value max_execution_time 45 Change the value (in seconds) or set to 0 for infinite time

3.3. Configuration in PHP scripts This is probably the best method as the configuration applies only to the particular script and would not allow other poorly written scripts to also consume and waste system resources. To do this, call the following function in your PHP script with the maximum execution time in seconds as a parameter: set_time_limit (300) You can use 0 as a parameter for infinite execution time.

Leave a Reply

Your email address will not be published. Required fields are marked *