問題描述
我正在嘗試將大文件上傳到我的服務器(我的服務器支持 post_max_size
192mb 和 max_execution_time
600 秒).當我上傳 100mb 文件時,執行將在 600 秒后停止,因此文件不會上傳到服務器.如何在 PHP 中增加 max_execution_time
(僅用于文件上傳過程)?我試過了:
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?
推薦答案
將此添加到 htaccess 文件中(并查看下面添加的編輯注釋):
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>
其他資源和信息:
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 年
隨著 PHP 和 Apache 的發展和壯大,我認為花點時間提及一些需要考慮的事情和可能的陷阱"對我來說很重要.考慮:
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 可以作為模塊或 CGI 運行.不建議以 CGI 運行,因為它為攻擊向量創造了很多機會 [閱讀更多].如果加載了
中的特定模塊,作為模塊運行(更安全的選項)將觸發要使用的設置. - 答案表明在第一行寫
mod_php5.c
.如果您使用的是 PHP 7,則將其替換為mod_php7.c
. - 有時在更改 .htaccess 文件后,重新啟動 Apache 或 NGINX 將不起作用.最常見的原因是您正在運行 PHP-FPM,它作為一個單獨的進程運行.您也需要重新啟動它.
- 請記住,這些設置通常在您的
php.ini
配置文件中定義.此方法通常僅在您的托管服務提供商不授予您更改這些文件的權限時才有用.在您可以編輯 PHP 配置的情況下,建議您在那里應用這些設置. - 最后,需要注意的是,并非所有 php.ini 設置都可以通過 .htaccess 文件進行配置.php.ini 指令的文件列表可以在在這里找到,并且唯一可以更改的是可更改列中具有模式 PHP_INI_ALL 或 PHP_INI_PERDIR 中的那些.
- 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 withmod_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.
這篇關于在 PHP 中增加 max_execution_time ?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!