簡體   English   中英

如何增加php中的執行超時?

[英]How to increase the execution timeout in php?

如何增加事務超時? 我想上傳視頻,但沒有上傳大尺寸的視頻?

它拋出錯誤The process *** exceeded the timeout of 60 seconds.

您需要更改 php.ini 中的一些設置:

upload_max_filesize = 2M 
;or whatever size you want

max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds

PHP.ini 的位置取決於您的系統環境。 欲了解更多信息:http: //php.net/manual/en/ini.list.php

您也應該能夠在運行時使用

set_time_limit(100);

http://php.net/manual/en/function.set-time-limit.php

或在您的虛擬主機配置中

php_admin_value max_execution_time 10000

對於不太可靠的應用程序的性能原因,將全局執行時間限制設置為 LOW 主要是一個好主意。 因此,您可能只想讓那些腳本運行得更長,而這絕對是必須的。

ps:不要忘記 post_max_size 和 upload_max_filesize (就像第一個答案告訴allready)

完成漢內斯的回答。

您需要更改 php.ini 中的一些設置:

upload_max_filesize = 2M 
;or whatever size you want

max_execution_time = 60
; also, higher if you must

如果有人想無限次輸入(我不知道為什么,但如果你願意),你可以將時間設置為 0:

您需要更改 php.ini 中的一些設置:

upload_max_filesize = 0 

max_execution_time = 0

如果你不知道你的 php.ini 在哪里。 您可以在服務器中創建一個文件“name.php”並輸入:

<?php phpinfo(); ?>

在您的網站上,您可以看到 php.ini 的配置,並標明它在哪里。

2015 年 1 月 9 日編輯:

如果您無法訪問您的 php.ini,您還有兩個選擇。

您可以直接在“name.php”文件中設置此行,但我找不到此選項的 upload_max_filesize:

set_time_limit(0);

或在“.htaccess”中

php_value upload_max_filesize 0
php_value max_execution_time 0

如果您需要做的僅針對 1 或 2 頁,我建議使用set_time_limit這樣它不會影響整個應用程序。

set_time_limit(some_values);

但當然這兩個值(post_max_size 和 upload_max_filesize)需要調查。

您可以通過ini_set函數設置它

ini_set('post_max_size','20M');
ini_set('upload_max_filesize','2M');

或直接在 php.ini 文件中,如 Hannes 上面的響應,甚至將其設置為 iin .htaccess,如下所示

php_value upload_max_filesize 2M
php_value post_max_size 20M

如果你碰巧使用的是微軟的IIS服務器,除了別人提到的php.ini設置外,你可能還需要在IIS服務器管理器中增加PHP FastCGI應用程序的執行超時設置:

步驟 1) 打開 IIS 服務器管理器(通常在開始菜單中的服務器管理器下,然后是工具/ Internet 信息服務 (IIS) 管理器)。

步驟 2) 單擊主連接(不特定於任何特定域)。

步驟 3) 在IIS部分下,找到FastCGI 設置(如下所示)。

在此處輸入圖像描述

步驟 4) 在其中,右鍵單擊 PHP 應用程序並選擇Edit...。

步驟 5) 檢查超時(如下所示)。

在此處輸入圖像描述

就我而言,這里的默認超時時間是 70 秒和 90 秒; 前者導致 PHP 腳本出現500 內部服務器錯誤,耗時超過 70 秒。

作為上述答案的補充,您可以使用set_time_limit()函數:

http://php.net/manual/en/function.set-time-limit.php

傳遞0作為參數將使您的腳本無時間限制地運行。

如果您無法編輯 php.ini(例如在您的服務器上),您可以嘗試從您的 php 代碼中更改 php.ini 參數。 嘗試:

ini_set('max_execution_time', 'NUMBER OF SECONDS TO ALLOW BEFORE TIMEOUT');

如果這不起作用,請嘗試以相同的方式設置“set_time_limit”,除此之外,我想說您唯一的選擇是聯系您的主機。 在安全模式下無法修改這些設置。

你有一個錯字: ini_set('max_input_time','200M') - 值集需要是一個 int,比如 ini_set('max_input_time','200')

首先通過phpinfo()檢查php.ini文件路徑; 然后更改PHP.INI參數:

upload_max_filesize = 1000M
memory_limit = 1500M
post_max_size = 1500M
max_execution_time = 30

重新啟動 Apache

set_time_limit(0); // safe_mode is off

ini_set('max_execution_time', 500); //500 seconds

注意:您也可以使用命令在 Linux 中查找php.ini

locate `php.ini`

我知道您是在專門詢問 PHP 超時,但似乎沒有其他人提到的是 Web 服務器上也可能存在超時,它看起來與 PHP 超時非常相似。

因此,如果您嘗試過:

  1. 通過添加一行來增加 php.ini 中的超時時間: max_execution_time = {秒數,即一分鍾 60 秒}
  2. 通過添加來增加腳本本身的超時時間: ini_set('max_execution_time','{number of seconds ie 60 for one minute}');

並且您已經使用 phpinfo() 函數檢查了 max_execution_time 確實增加了,那么您可能想嘗試將其添加到 .htaccess 中,這將確保 Apache 本身不會超時:

RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]

更多信息在這里: https ://www.antropy.co.uk/blog/php-script-keeps-timing-out-despite-ini-set/

測試您是否處於安全模式 - 如果不是 - 將時間限制(本地值)設置為您想要的:

if(!ini_get('safe_mode')){

    echo "safe mode off";
    set_time_limit(180);// seconds

    phpinfo();// see 'max_execution_time'
}

*如果安全模式“開啟”,您無法以這種方式設置時間限制。

可選:如果您設置了關於 php.ini 的配置但仍然無法上傳

- 這是檢查錯誤代碼的 php 函數

$error_type = [];
$error_type[0] = "There is no error";
$error_type[1] = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
$error_type[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
$error_type[3] = "The uploaded file was only partially uploaded.";
$error_type[4] = "No file was uploaded.";
//$error_type["5"] = "";
$error_type[6] = "Missing a temporary folder. Introduced in PHP 5.0.3.";
$error_type[7] = "Failed to write file to disk. Introduced in PHP 5.1.0.";
$error_type[8] = "A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.";
//------------------------------
//--> show msg error.
$status_code = $_FILES["uploadfile"]["error"];
if($status_code != 0){
    echo $error_type[$status_code];
    exit;
}

您還可以在.htaccess文件中設置最大執行時間:

php_value max_execution_time 180

為了真正增加時間限制,我更喜歡首先獲得當前值。 set_time_limit並不總是增加時間限制。 如果當前值(例如來自php.ini或以前設置的值)高於當前調用set_time_limit時使用的值,它將減少時間限制!

那么像這樣的小幫手是怎么回事呢?

/**
 * @param int $seconds Time in seconds
 * @return bool
 */
function increase_time_limit(int $seconds): bool
{
    return set_time_limit(max(
        ini_get('max_execution_time'), $seconds
    ));
}

// somewhere else in your code
increase_time_limit(180);

另請參閱: 在 PHP 腳本中獲取 max_execution_time

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM