文件上传可以在 PHP 中超时吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/578190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can file uploads time out in PHP?
提问by Ben McRae
Hi im quite new to PHP, i have created a form for very large csv files to be uploaded to my server. Some one mentioned to me that the browser can time out due to the uploading file being to big, is this true? and if so, can it be prevented?
嗨,我对 PHP 很陌生,我已经为要上传到我的服务器的非常大的 csv 文件创建了一个表单。有人跟我说浏览器会因为上传文件过大而超时,这是真的吗?如果是这样,是否可以预防?
Thanks for your help!
谢谢你的帮助!
回答by Ionu? G. Stan
You need a proper value for the following php.ini settings:
您需要为以下 php.ini 设置设置正确的值:
- max_input_time(notmax_execution_time!)
- upload_max_filesize
- post_max_size
- max_input_time(不是max_execution_time!)
- 上传最大文件大小
- post_max_size
and maybe
有可能
回答by Gumbo
There are some configuration directives that can cause large uploads to fail if their values are too small:
如果它们的值太小,有一些配置指令可能会导致大型上传失败:
PHP
PHP
max_input_time???Maximum time in seconds a script is allowed to parse input data, like POST, GET and file uploadsupload_max_filesize???Maximum size of an uploaded file.post_max_size???Maximum size of post data allowed.
max_input_time???允许脚本解析输入数据的最长时间(以秒为单位),例如 POST、GET 和文件上传upload_max_filesize???上传文件的最大大小。post_max_size???允许的最大帖子数据大小。
Apache
阿帕奇
TimeOut???Amount of time the server will wait for certain events before failing a requestLimitRequestBody???Restricts the total size of the HTTP request body sent from the client
TimeOut???服务器在请求失败之前等待特定事件的时间LimitRequestBody???限制客户端发送的HTTP请求体的总大小
There are probably some more than this.
可能还有更多。
回答by Ronny Vindenes
A good way to work around the poor handling of large file uploads in php, is to use an uploader like JUploadwhich will split the file into chunks before sending them. This also has the benefit for your users that they get a proper progress feedback while uploading, and they can upload multiple files in one go.
解决 php 中大文件上传处理不当的一个好方法是使用像JUpload这样的上传器,它会在发送之前将文件分成块。这对您的用户也有好处,他们在上传时获得适当的进度反馈,并且他们可以一次性上传多个文件。
回答by Jim B
I was able solve this problem using the following settings, you could use different values but you get the idea:
我能够使用以下设置解决这个问题,你可以使用不同的值,但你明白了:
For my server, I put these lines in a ".user.ini" file inside the script directory, your server may look for a different file, if you do a phpinfo('user_ini.filename') on the server it will spit out the file you need to put your values in
对于我的服务器,我将这些行放在脚本目录内的“.user.ini”文件中,您的服务器可能会寻找不同的文件,如果您在服务器上执行 phpinfo('user_ini.filename') 它将吐出您需要将值放入的文件
max_execution_time = 1800
max_input_time = -1
post_max_size = 100M
upload_max_filesize = 100M
memory_limit = 256M
回答by vartec
When uploading very large files, you have to change 4 configuration variables:
上传非常大的文件时,您必须更改 4 个配置变量:
upload_max_filesizepost_max_sizememory_limittime_limit
upload_max_filesizepost_max_sizememory_limittime_limit
Time limit may be increased at runtime with set_time_limit().
可以在运行时使用 set_time_limit() 增加时间限制。
回答by Beep beep
A script is allowed to run, by default, for something like 30 seconds. You can use the set_time_limit() functionto alter this. Also, if your user will need to upload large files, you'll need to change the post_max_size and/or the upload_max_filesize values in your php.ini file.
默认情况下,允许脚本运行大约 30 秒。您可以使用set_time_limit() 函数来改变它。此外,如果您的用户需要上传大文件,您需要更改 php.ini 文件中的 post_max_size 和/或 upload_max_filesize 值。
Also, if you want to just extend your timeout limit globally, you can change max-execution-time in php.ini.
此外,如果您只想全局扩展超时限制,您可以在 php.ini 中更改 max-execution-time。
回答by Cory R. King
My answer is not directly related to your original question, but if you have a reverse proxy load balancer in front of your PHP script, the load balancer can timeout or block large uploads. Always check your load balancer's configuration if you support file uploads. Just like PHP, most load balancers default settings for uploads are pretty small.
我的回答与您的原始问题没有直接关系,但是如果您的 PHP 脚本前面有反向代理负载均衡器,负载均衡器可能会超时或阻止大上传。如果您支持文件上传,请务必检查负载均衡器的配置。就像 PHP 一样,大多数负载均衡器的默认上传设置都非常小。
回答by Ates Goral
Yes it is true. File upload is done through a POST request and requests in general are subject to timeout. You should be able to reconfigure your environment for a longer request timeout.
是的,它是真的。文件上传是通过 POST 请求完成的,并且请求通常会超时。您应该能够为更长的请求超时重新配置您的环境。
回答by macleojw
It's not just timeouts that can cause problems. There are some limits on the maximum size of file that can be uploaded. These limits can be changed in the php.ini file:
不仅仅是超时会导致问题。可以上传的文件的最大大小有一些限制。这些限制可以在 php.ini 文件中更改:
post_max_size
upload_max_filesize
memory_limit
post_max_size
upload_max_filesize memory_limit
Check out http://uk.php.net/ini.corefor details.
查看http://uk.php.net/ini.core了解详情。
回答by David Robertson
If changing any of the above parameters doesn't seem to make any difference, it can be that a html form somewhere contains the name MAX_FILE_SIZE as a hidden field.
如果更改上述任何参数似乎没有任何区别,则可能是某处的 html 表单包含名称 MAX_FILE_SIZE 作为隐藏字段。
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
In the example above, any file over 10MB will not be uploaded.
在上面的例子中,任何超过 10MB 的文件都不会被上传。

