php 无法上传大文件完美解决方案
php 无法上传大文件完美解决方案
php.ini无法上传大文件完美解决办法
1、打开php.ini(打开方式就不用说了,百度一大堆)
2、查找post_max_size
表单提交最大数值,此项不是限制上传单个文件的大小,而是针对整个表单的提交数据进行限制的
默认为8M,设置为自己需要的值,此参数建议要设置比upload_max_filesize大一些
3、查找File Uploads
是否允许通过http上传文件的开关,确认file_uploads = on
4、查找upload_tmp_dir
文件上传至服务器上存储临时文件的地方,如果没指定就会用系统默认的临时文件夹
如果系统报错提示有“XXX临时目录XXX”的话,这个目录就需要你来设置一个有效目录,没报错就不用管
5、查找upload_max_filesize
允许上传文件大小的最大值,默认为2M,设置为自己需要的值此参数建议不要超过post_max_size值,因为它受控于post_max_size值(就算upload_max_filesize设置了1G,而post_max_size只设置了2M时,大于2M的文件照样传不上去,因为它受控于post_max_size值)
6、如果要上传大于8M的文件,还需要对下面的参数也进行设置:
查找max_execution_time = 600 ;每个PHP页面运行的最大时间值(秒),默认30秒
max_input_time = 600 ;每个PHP页面接收数据所需的最大时间,默认60秒
memory_limit = 8M ;每个PHP页面所需要的最大内存,默认8M
nginx法上传大文件完美解决办法
1.client_body_buffer_size 尽量设置的大点,这是基于速度的考虑,如果因为设置的过小,导致上传的文件老要写磁盘,那速度就太慢了。
2.client_body_temp_path 路径要有可写权限,这个是明显的错误了。改正了就好
3.client_max_body_size 设置上传文件的最大值,这个是基于安全的考虑,我们认为正常用户不会或者基本不会上传太大的文件。
可以设置为client_max_body_size 100m; 或者按照自己的业务来设置这个值。
例子
nginx的配置。
代码如下 | |
cd /export/servers/nginx/conf/nginx.conf,在这个配置文件里面的server段里面的 location / { root html; index index.html index.htm; client_max_body_size 1000m; } |
加上了client_max_body_size 字段,怎么重启都不行。后来在总配置文件里面发现了分配置文件:
代码如下 | |
sendfileon; |
于是找到了分配置文件,在分配置文件里面进行修改。分配置文件配置如下:
代码如下 | |
server { listen 80; server_name chat.erp.360buy.com; # access_log /export/servers/nginx/logs/chat.erp.360buy.com; location / { proxy_pass http://tomcat; client_max_body_size 1000m; } } |
用/export/servers/nginx/sbin/nginx -s reload重启下,上传文件的大小受限的问题就解决了。
分享下我的解决过程,希望对大家有帮助。
如果还是无法解决我们需要看看是不是php表单设置问题
这段代码分为两个文件,一个为upload.html,一个是upload.php
upload.html
代码如下 | |
其中,
请注意
这是一个标签,我们要实现文件的上传,必须指定为multipart/form-data,否则服务器将不知道要干什么。
值得注意的是文件upload.html中表单选项 MAX_FILE_SIZE 的隐藏值域,通过设置其Value(值)可以限制上载文件的大小。
MAX_FILE_SIZE 的值只是对浏览器的一个建议,实际上它可以被简单的绕过。因此不要把对浏览器的限制寄希望于该值。实际上,PHP 设置中的上传文件最大值,是不会失效的。但是最好还是在表单中加上 MAX_FILE_SIZE,因为它可以避免用户在花时间等待上传大文件之后才发现该文件太大了的麻烦。
upload.php
代码如下 | |
$f=&$HTTP_POST_FILES['Myfile']; $dest_dir=’uploads’;//设定上传目录 $dest=$dest_dir.’/’.date("ymd")."_".$f['name'];//设置文件名为日期加上文件名避免重复 $r=move_uploaded_file($f['tmp_name'],$dest); chmod($dest, 0755);//设定上传的文件的属性 或者 |
以上范例中 $_FILES 数组的内容如下所示。我们假设文件上传字段的名称为 userfile(名称可随意命名)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
