Fatal error: Maximum execution time of 30 seconds

2019-02-20 18:26发布

问题:

I have a customer who is getting an error related to a script timeout. I've included the code below, but i think the issue is that the timeout needs to be extended.

Is this something I can set in the code, or does it have to be set by the web host (GoDaddy)?

Fatal error: Maximum execution time of 30 seconds exceeded in D:\hosting\123\html\siteame\wp-content\plugins\myplugin\myplugin.php on line 170

The code at that point is:

function my_copy_recurse($src,$dst){
    $dir = opendir($src); 
    @mkdir($dst); 
    while(false !== ( $file = readdir($dir)) ) 
    { 
        if (( $file != '.' ) && ( $file != '..' )) 
        { 
            if ( is_dir($src . '/' . $file) ) { 
                my_copy_recurse($src . '/' . $file,$dst . '/' . $file); 
            } 
            else { 
                copy($src . '/' . $file,$dst . '/' . $file); 
            } 
        } //THIS IS LINE 170
    } 
    closedir($dir); 
}

回答1:

To limit the maximum execution time use set_time_limit($seconds). If set $seconds to zero, no time limit is imposed. So just add set_time_limit(0) at the beginning of the script and script will work till the end. But if user's browser disconnects due to browser timeout your script could be halted, so you need to add ignore_user_abort(true) at the beginning of the script to ignore it and work exactly till the end of the script.



回答2:

If you've hosted your site on GoDaddy, Windows Hosting and are using WordPress, sooner or later you will face the max execution time out problem. I faced this problem for quite some time and looked aroung google, hitting my head. If you've faced the same series of problems as below:

  1. You are hosted on GoDaddy thinking they are awesome, but you find out wordpress throws execution time errors
  2. you contacted support. they clearly give you either a cold shoulder or ask you to write a custom php.ini/php5.ini.
  3. you dont know what to do and your boss is on your head.
  4. you try to get hold of GoDaddy's ini file, you cannot!
  5. Finally, you decide to write your own PHP.ini file but are either afraid of breaking something or just do not know where to start,

this post is for you>

  1. open up your text editor. I recommend notepad++. create the following file: info.php
  1. upload info.php to the root of your hosting. Now browse to http://yourhost/info.php. Whatever loads up on the screen is the PHP config. Look for Loaded Configuration File and note down its value. IN my case it is C:\PHP5\php.ini. We need all this information in our custom PHP5.ini file but its too much info to copy and format, so we will copy GoDaddy's default file and edit it as necessary.

  2. fire your text editor and create the following file: getconfig.php <?php copy('C:\PHP5\php.ini'*, 'PHP5.ini'); echo('finally'); ?>
    (replace * with whatever path shows up in Loaded Configuration File)

  3. Open up GoDaddy Hosting Manager (yes via their website). Launch the File Manager
  4. On your hosting root create a folder "baddaddy".
  5. select newly created folder "baddaddy" and click permissions. Make it web writable.
  6. copy file getconfig.php to badaddy and now browse to http://yourhost/baddaddy/getconfig.php.

  7. Once you see "finally" written on screen, go back to the filemanager in GoDaddy Control Panel, goto baddaddy folder and you will see a new file PHP5.ini appear there. Download this file. This is a copy of GoDaddy Settings.

  8. Make whatever changes you need to(just know what you're changing, I am not a PHP expert, so I cannot be responsible if you screw up something). For my problem, I changed the max_execution_time = 30 to max_execution_time = 1200.

  9. Upload the edited file to the root of your application and back in the GoDaddy Control Panel, IIS Management, hit Recycle App Pool, and then WAIT FOR A WHILE (20 minutes)

  10. browse back to http://yourhost/info.php to confirm the new settings are in place

IMPORTANT ** IF you see a "500: Internal Server Error" on your GoDaddy WordPress Blog after doing all this, 12. Change the log_errors from log_errors = On to log_errors = Off in the PHP5.ini you just edited and re-upload, recycle app pool and wait. The 500 error pop up if the log file cannot be found.

I AM NO PHP EXPERT but this thing worked for me, the blog still loads slow but I have not seen any "Fatal Error: 30 second timeout" since then.



回答3:

Depending on what settings your host has allowed you may be able to use set_time_limit() pass to it in seconds how long you want it to spend before it times out (use 0 for infinite). E.g:

set_time_limit(0);     //script will now run till completes

GoDaddy may have blocked this though so if it doesn't work try contacting them



回答4:

Girish's answer is incomplete. What has to be included in info.php? You may use simple update provided at: fix-for-fatal-error-maximum-execution-time-30-seconds-exceeded-wordpress



回答5:

By setting "max_execution_time = 600" in "php.ini" file worked for me.



回答6:

Change your code to this :

function my_copy_recurse($src,$dst){
    $dir = opendir($src); 

    @mkdir($dst); 

    while($file = readdir($dir) ) 

    { 

        if ($file == '.' || $file == '..' ) continue; 


            if ( is_dir($src . '/' . $file) ) { 
                my_copy_recurse($src . '/' . $file,$dst . '/' . $file); 
            } 
            else { 
                copy($src . '/' . $file,$dst . '/' . $file); 
            } 
    } 
    closedir($dir); 
}


标签: php timeout