如何解决PHP文件上传限制的问题

2023-06-1017:39:54后端程序开发Comments992 views字数 1488阅读模式

上传文件的时候经常会遇到上传失败,而失败的原因有很多种,这其中就包括nginx对文件上传的限制,还有PHP对文件上传的限制。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

注意了,这里说的限制不只是大小的限制,还有上传时间等。今天我们就来说说Nginx和PHP对文件上传的相关限制参数吧?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

Nginx相关限制文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

对于web而言nginx是最上层的代理,对于上传的限制有以下几种:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

1. client_max_body_size文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

客户端请求服务器最大允许大小,在“Content-Length”请求头中指定。如果请求的正文数据大于client_max_body_size,HTTP协议会报 413 Request Entity Too Large 错误。就是说如果请求的正文大于client_max_body_size,一定是失败的。如果需要上传大文件,一定要修改该值。默认值:1M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

2. client_body_timeout文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

读取请求实体的超时时间,若超过所设定的大小,返回413错误。默认值:60秒文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

3. client_header_timeout文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

读取请求头的超时时间,若超过所设定的大小,返回408错误。默认值:60秒文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

和上面 client_body_timeout 读取body体一样,仅读取 header头部的时间。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

4. proxy_connect_timeout文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

http请求无法立即被容器(tomcat, php-fpm等)处理,被放在nginx的待处理池中等待被处理。此参数为等待的最长时间,默认为60秒,官方推荐最长不要超过75秒文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

5. proxy_read_timeout文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

http请求被容器(tomcat, php-fpm等)处理后,nginx会等待处理结果,也就是容器返回的response。此参数即为服务器响应时间,默认60秒文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

6. proxy_send_timeout文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

http请求被服务器处理完后,把数据返回给Nginx的用时,默认60秒。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

PHP上传相关限制文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

1. file_uploads文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

服务器上的PHP脚本是否可以接受HTTP文件上传。On开启,Off关闭文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

2. max_execution_time文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

页面最长执行时间,默认为30秒。即使页面上传时设置超时时间超过这个值,30秒之后也会中断请求,页面报错。与PHP中的 set_time_limit 有相同的作用,但是set_time_limit需要关闭安全模式才起作用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

配置为0,表示不做时间限制文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

3. post_max_size文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

定义POST上传数据时最大可上传的大小,默认为2M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

如果实际POST过来的数据大于该值,则 $_POST和 $_FILES 这两个全局变量就为空值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

示例:post_max_size = 50M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

4. upload_max_filesize文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

定义上传文件时可接受的文件大小的最大值。默认为8M。这个根据实际业务场景配合Nginx进行调整文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

示例:upload_max_filesize = 50M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

5. upload_max_filesize文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

最大上传文件的大小,此值必须不大于post_max_size值文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

示例:upload_max_filesize=8M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

6.upload_tmp_dir文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

上传时临时目录,默认为空,系统指定文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

示例:upload_tmp_dir = '/tmp/phpupload'文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

7. max_input_time文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

PHP脚本从客户端接受请求数据的最大时间,这个时间包含了POST、GET和文件上传等数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

示例:max_input_time = 120文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

8. memory_limit文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

单个PHP脚本能申请到的最大内存空间。有的时候会因为使用内存超出限制,导致程序出错,可以适当的增大该值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

memory_limit = 100M文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/46828.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/46828.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定