PHP defaults to a PHP memory limit of 16MB. Some websites may require more memory than 32MB. It is not uncommon for sites to require memory limits higher than 64MB. The default upload file limit of 2MB is quite low, particularly if the site uses photos that can easily exceed 32MB in size. Script Execution Time is 30 seconds by default. However, some tasks (e.g. service cron tasks), require more time. There are many ways to increase PHP memory, upload file limit, or execution time. You only need one. Your system configuration will determine which one is best for you.
1. Limit the memory
If you have access to the server’s php.ini, this is the best approach. It will not work in shared hosting environments. However, your host might be able to adjust it for you. This change will impact all websites and PHP scripts that are hosted on the server. – Find the php.ini file that is used by your web server. To locate it, you can use the PHP function phpinfo(). – Edit memory_limit in the PHP.ini file. This is usually in a section called Resources Limits. memory_limit = 32M. Maximum memory that a script can consume (32MB). If there isn’t a section for this, add the line above to the end of your file. Restart Apache. Note: You may have two PHP.ini files if you use XAMPP/WAMP. One is under the PHP directory, the other under Apache/bin. Edit the file in the XAMPP/Apache/bin directory to change your memory limit. These solutions may not be applicable to all sites but are more limited in scope.
1.2. .htaccess configuration
If you don’t have access to the php.ini file, this is helpful. Edit (Create the .htaccess) file located in the site root directory (public), and add the following line to it: php_value 32M. This method is only applicable if PHP runs 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 for Shared Hostings
Some shared hosting environments restrict access to the PHP memory limit setting. If you are unable to make the change yourself please contact your hosting provider or find a host that offers more flexibility.
2. Upload file
Size limits can be set by your PHP installation to limit the upload file size. The default limit will be 2MB for uploading files. You will need to configure the following configuration options: – upload_max_filesize– Maximum file size for an uploaded file. – memory_limit: This determines the maximum memory a script can allocate in bytes. This prevents poorly written scripts from consuming all the memory available on a server. This directive can be set to -1 if you do not want to exceed the memory limit. – post_max_size: This setting determines the maximum size of any post data that can be uploaded. This setting affects file upload. Uploading large files will require this value to be greater than upload_max_filesize. Memory_limit can also be used to limit file uploading if memory limit has been enabled in your configure script. Memory_limit should be greater than post_max_size.
This problem can be fixed by two different methods.
2.1. PHP.ini configuration
Locate the php.ini files used by your web server. To locate it, you can use the PHP function phpinfo(). – modify the next parameters memory_limit = 32M upload_max_filesize= 10M post_max_size= 20M – Restart Apache
2.2. .htaccess configuration
If you don’t have access to the php.ini file, this is helpful. Edit (Create the .htaccess) file located in the site root directory. Add the following line: PHP_value upload_max_filesize 5M PHP_value post_max_size 15M PHP_value memory_limit 32M. This method is only applicable if PHP runs as an Apache module.
3. Execution Time
You can increase the script execution time PHP scripts cannot be executed for longer periods of time. If it exceeds the time limit, it will produce the following error message: “Fatal error. Maximum execution time of yourscript.php exceeded”. To make your PHP script run for longer times, you can use any of these methods to increase the PHP script’s maximum execution time limit.
3.1. PHP.ini configuration
Locate the php.ini files used by your web server. To find it, you can use the PHP function phpinfo(). – Find and modify the next parameter: max_execution_time =45 Change the value (in seconds) or set it to 0 for an infinite time. – Restart Apache
3.2. .htaccess configuration
.htaccess configuration Edit or Create the .htaccess File in the site root (public), or in the directory for your script. Add the following line: PHP_value max_execution_time45 Change the value (in seconds) or set it to 0 for an infinite time
3.3. Configuration in PHP scripts
This is the most efficient method because it applies only to that script and does not allow any other scripts to consume or waste the system resource. For infinite execution, you can call the following PHP function with set_time_limit (300 ) as the parameter.