Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase max_execution_time in PHP?

Tags:

I'm trying to upload large files to my server (my server support post_max_size 192mb and max_execution_time 600 sec). When I upload 100mb files execution will stop after 600 sec so files are not uploaded to the server. How can I increase max_execution_time in PHP (only for the file uploading process)? I have tried:

ini_set ( 'max_execution_time', 1200);  if(move_uploaded_file($_FILES['Video']['tmp_name'],$tmppath)) {   // ... }  

Any ideas how I can overcome this?

like image 787
Chithri Ajay Avatar asked Jan 05 '12 14:01

Chithri Ajay


People also ask

How can we increase the execution time of PHP script?

To increase the script execution time, you can use the following snippet at the top of your script. ini_set ( 'max_execution_time' , '300' ); In the above example, it would set the max_execution_time directive to 300 seconds.

What is max_execution_time in PHP INI?

max_execution_time int. This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30 . When running PHP from the command line the default setting is 0 .

What is the best value for max_execution_time in PHP?

max_execution_time is set to 60 which is too low. A minimum execution time of 150 seconds is recommended to give the migration process the best chance of succeeding. If you use a managed host, contact them directly to have it updated.


1 Answers

Add this to an htaccess file (and see edit notes added below):

<IfModule mod_php5.c>    php_value post_max_size 200M    php_value upload_max_filesize 200M    php_value memory_limit 300M    php_value max_execution_time 259200    php_value max_input_time 259200    php_value session.gc_maxlifetime 1200 </IfModule> 

Additional resources and information:

  • https://www.php.net/manual/en/info.configuration.php

  • https://www.looklinux.com/how-to-set-php-maximum-execution-time-in-an-htaccess-file/


2021 EDIT:

As PHP and Apache evolve and grow, I think it is important for me to take a moment to mention a few things to consider and possible "gotchas" to consider:

  • PHP can be run as a module or as CGI. It is not recommended to run as CGI as it creates a lot of opportunities for attack vectors [Read More]. Running as a module (the safer option) will trigger the settings to be used if the specific module from <IfModule is loaded.
  • The answer indicates to write mod_php5.c in the first line. If you are using PHP 7, you would replace that with mod_php7.c.
  • Sometimes after you make changes to your .htaccess file, restarting Apache or NGINX will not work. The most common reason for this is you are running PHP-FPM, which runs as a separate process. You need to restart that as well.
  • Remember these are settings that are normally defined in your php.ini config file(s). This method is usually only useful in the event your hosting provider does not give you access to change those files. In circumstances where you can edit the PHP configuration, it is recommended that you apply these settings there.
  • Finally, it's important to note that not all php.ini settings can be configured via an .htaccess file. A file list of php.ini directives can be found here, and the only ones you can change are the ones in the changeable column with the modes PHP_INI_ALL or PHP_INI_PERDIR.
like image 172
Jeremy Harris Avatar answered Oct 15 '22 06:10

Jeremy Harris