Setting the maximum execution time and memory limit when PHP is running in safe mode

24 May

Many php applications, like WordPress Backup to Dropbox, require an extended max execution time and memory limit PHP settings in order to complete a task. By default the time setting is set to 30 seconds and, in the case of my WordPress plugin, is not enough for a backup to be completed.

If the PHP installation was not compiled in safe mode then a developer can temporarily change this value within a plugin using set_time_limit. So for any long running scripts setting the time limit to zero will tell PHP to allow the script to run forever.

However if your PHP installation is running in safe mode, this functionality is blocked and there is no way of altering the time or memory limit from within the application. Therefore the only way to get the an application to complete its task the php.ini max_execution_time and memory_limit setting will need to be altered. In the case of WordPress Backup to Dropbox this setting needs to be set to at least 3600 seconds  (1 hour) and at least 150M respectively.

How easily this value can be changes depends on how your hosts PHP installation is configured. If your host allows it you can put a file named php.ini inside the root and wp-admin folders of your WordPress installation with the settings set to

max_execution_time = 0
memory_limit = 150M

If this does not work then you have to edit the value in your php.ini file. The location of this file depends on your operating system and a quick Google search will yield plenty of literature on how this is done.

 
  1. Friedbert

    November 22, 2015 at 8:53 pm

    Hello Michael,
    I suffer from the well known timeout problem, but my provider does not allow to change php.ini. Also the PHP installation does not seem to be running in safe mode, as php.ini shows “safe_mode = Off”.
    How can I fix this?
    In addition, the database backup file created is about 800kByte only. A full SQL dump by phpAdmin is about 2 MByte. Does your plugin copy the whole database or only parts?
    Thank you very much in advance.
    Friedbert

     
    • Mikey

      November 28, 2015 at 8:32 pm

      Gday Friedbert,

      I am working on solving the timeout issue. If your PHP is not running in safe mode then the plugin should run better so you dont need to change anything here.

      Are you dumping just the WordPress database file using phpMyAdmin? The SQL dump is of the WordPress database table only.

      Cheers,
      Mikey

       
  2. Salomon

    September 28, 2015 at 5:57 am

    Hi mike

    im tryng to start using this awesome plugin but always get this error

    “Backup error on 2015-09-27 at 02:56:20: The backup process appears to have gone away. Resuming backup.
    Backup started on 2015-09-27 at 02:55:22
    Backup error on 2015-09-27 at 02:55:21: The backup process appears to have gone away. Resuming backup.
    Backup started on 2015-09-27 at 02:54:20
    Backup error on 2015-09-27 at 02:54:20: The backup process appears to have gone away. Resuming backup.
    Backup started on 2015-09-27 at 02:54:20″

    pls help, what should i do ???

    Tx in advance

     
  3. WordPress Backup to Dropbox | Wbcom Designs | WordPress Developer

    September 14, 2015 at 11:44 pm

    [...] Yes you can, however you need to modify the max execution time in your php.ini manually. Please read this blog post for more information. [...]

     
  4. WordPress Backup to Dropbox | WordPress Designer

    September 14, 2015 at 10:49 pm

    [...] you can, however you need to modify the max execution time in your php.ini manually. Please read this blog post for more information. Share this:DiggFacebook « Back to [...]

     
  5. Curso de php

    August 11, 2015 at 5:45 am

    Very Good. Thank you!

     
  6. Matt Auckland

    July 6, 2015 at 1:06 am

    Just thought this might be useful for you in regards to memory usage and file uploading.

    When I recently created an image handling class for a cms, I wanted to be able to process large resolution images up to and beyond 3000px. So to do this I had to work around the execution time and memory issues.

    So firstly I didn’t want the memory to be allocated all the time, just when I needed it. So in the construct of my image class, I used the following lines:

    /** zero the script execution time and set the maximum amount of memory this script may consume */
    ini_set( ‘max_execution_time’, 0 );
    ini_set( ‘memory_limit’, ’128M’ );

    But this doesn’t solve the upload issue. For that you need to set the upload_max_filesize and post_max_size in PHP ini.

    Now not everyone has access to PHP ini, and also you don’t want these settings on full time, so to solve this issue I added the lines to the htaccess file for the admin section, like so;-

    # Set some php.ini defaults
    php_value upload_max_filesize 10M
    php_value post_max_size 40M

    So now I can upload images no bigger than 10MB, and post form values of no more than 40MB. Why 40MB you ask? Well it is a guesstimate, but when submitting a form, any other text or data submitted also counts towards the post_max_size. So it is best to give it a little headroom to work with.

    Because I set the memory settings in the class, and the upload values in htaccess, they only take up the servers memory when that class, or admin section is in use. So it doesn’t cane the server and you don’t have to mess around with the PHP ini files. Bob is your uncle.

    Hope that proves useful.

     
    • Mikey

      July 6, 2015 at 9:06 am

      Gday Matt,

      Thanks for the information! The max_upload_size and post_max_size refers to a HTTP post from a client to a sever so in your case these values are important because your scripts will error if a user of your service attempts to upload a larger file. In Backup to Dropboxes case I am uploading from server to server using OAuth making these values redundant.

      Cheers,
      Mikey

       
  7. Marieke

    July 3, 2015 at 12:50 am

    Hi, my error message says:

    Backup error on 2015-07-02 at 02:16:55: Exception – Malformed response:
    Backup started on 2015-07-02 at 02:14:52

    Any ideas on that? Thanks.

     
    • Mikey

      July 6, 2015 at 9:11 am

      Gday Marleke,

      Does this happen all the time or just occasionally? If its the latter then its probably a one off error.

      Cheers,
      Mikey

       
  8. hello125

    June 2, 2015 at 12:42 am

    Hi!
    I see the following error:

    Warning: include(HTTP/OAuth/Consumer.php) [function.include]: failed to open stream: No such file or directory in /usr/export/www/vhosts/*****/hosting/******/blog/wp-content/plugins/wordpress-backup-to-dropbox/Dropbox_API/OAuth/PEAR.php on line 47

    Warning: include() [function.include]: Failed opening ‘HTTP/OAuth/Consumer.php’ for inclusion (include_path=’.:/usr/share/php’) in /usr/export/www/vhosts/***/hosting/*****/blog/wp-content/plugins/wordpress-backup-to-dropbox/Dropbox_API/OAuth/PEAR.php on line 47
    There was a fatal error loading WordPress backup to Dropbox

    The HTTP_OAuth_Consumer class could not be found! Did you install the pear HTTP_OAUTH class?

    What can i do?

     
    • Mikey

      June 3, 2015 at 9:43 am

      Gday hello125,

      It appears that your installation is not including the bundled PEAR includes correctly. This probably because your host is blocking the usage of the ini_set function.

      Try manually adding the following line to a php.ini file in the root of your WordPress directory:

      include_path=’.:/usr/share/php:/usr/export/www/vhosts/*****/hosting/******/blog/wp-content/plugins/wordpress-backup-to-dropbox/PEAR_Includes’

      Cheers,
      Mikey

       
      • hello125

        June 3, 2015 at 11:11 pm

        I have done it, but nothing happens…

         
        • vashte (@make1inc)

          October 22, 2015 at 6:59 am

          I am getting the following error – somewhat similar to the above:

          Warning: require_once() [function.require-once]: open_basedir restriction in effect. File(/usr/share/pear/PEAR/Exception.php) is not within the allowed path(s): (/var/www/vhosts/make-one.co/httpdocs:/tmp) in /var/www/vhosts/make-one.co/httpdocs/wp-content/plugins/wordpress-backup-to-dropbox/PEAR_Includes/HTTP/Request2/Exception.php on line 47

          Warning: require_once(/usr/share/pear/PEAR/Exception.php) [function.require-once]: failed to open stream: Operation not permitted in /var/www/vhosts/make-one.co/httpdocs/wp-content/plugins/wordpress-backup-to-dropbox/PEAR_Includes/HTTP/Request2/Exception.php on line 47

          Fatal error: require_once() [function.require]: Failed opening required ‘PEAR/Exception.php’ (include_path=’.:/usr/share/pear:/usr/share/php:/var/www/vhosts/make-one.co/httpdocs/wp-content/plugins/wordpress-backup-to-dropbox/PEAR_Includes’) in /var/www/vhosts/make-one.co/httpdocs/wp-content/plugins/wordpress-backup-to-dropbox/PEAR_Includes/HTTP/Request2/Exception.php on line 47

          I also tried modifying the php.ini file, but nothing is happening there either. This is a really random error – I haven’t touched the plugin at all, and it had been working fine for months.

           
          • Mikey

            October 25, 2015 at 8:05 am

            Gday vasthe,

            You host is restricting where PHP is allowed to get its include files from. Backup to Dropbox requires the PEAR include OAuth and its dependencies.

            In order to use the plugin you will need to have the bundled PEAR directory included in the safe list on your host or get them to install PEAR OAuth for you.

            Cheers,
            Mikey

             
  9. WordPress Backup to Dropbox 0.8 « Mikey D

    May 26, 2015 at 10:01 pm

    [...] mode enabled then there is no way this can be set in code. For more information please read the blog post I wrote about this [...]