Setting execution time and memory limits in php

When executing large amount of data through php scripts it is common facing errors like “Fatal error: Maximum execution time of 300 seconds exceeded..”. Also, sometimes uploading large files through php scripts you might face errors like “The uploaded file exceeds the upload_max_filesize directive in php.ini..” etc. etc.. The most possible reason for these kind of errors is the predefined default execution time and/or memory limit settings. In fact, in form of these kind of errors your system resources demand for more allocation of memory and/or time to complete operations asked.

Here are some simple measures to fix such errors and warnings by setting execution time and memory limits in php. To increase execution limit globally look for the following directives in php.ini file.

(To learn more about these directives see http://php.net/manual/en/ini.core.php)

post_max_size = 8M
upload_max_filesize = 2M
max_execution_time = 30
max_input_time = 60
memory_limit = 8M

(Generally these parameters are found in different lines of php.ini file so use ‘find’ utility of your text editor to find them, one at a time)

Change them to suit your needs. I made following changes to suit my needs:

post_max_size = 500M
upload_max_filesize = 500M
max_execution_time = 5000
max_input_time = 5000
memory_limit = 1000M

If you don’t have access to php.ini file, try setting up a .htaccess file with the following directives in it and place this file in your web server’s root folder. (If you want these settings to affect only a particular area of your website place this .htaccess file in that particular folder)

php_value post_max_size 500M
php_value upload_max_filesize 500M
php_value max_execution_time 5000
php_value max_input_time 5000
php_value memory_limit 1000

In case you are not able to modify ini.php on server or even you dont have permissions to setup a .htaccess file you may try to setup these values within your php script file. To make these settings take in effect your whole application just place them at the top of some common include file. In my case i set up them in my config.php file (only if needed) which i include at the top of every php file.

ini_set('post_max_size', '500M');
ini_set('upload_max_filesize', '500M');
ini_set('max_execution_time', 5000);
ini_set('max_input_time', 5000);
ini_set('memory_limit', 1000);

I hope that helps someone.

2 thoughts on “Setting execution time and memory limits in php

  1. i create a tree structure using jquery, getting data from mysql.but when click on nodes, system executation time exceeds 30 seconds.
    Any idea how to increase system time automatically based up on memory?
    I know max_execution_time().Is any other method in php?..
    reply fast…
    Thank you

Leave a Reply