PHP 文件上傳限制問題
PHP 大文件上傳占用大量資源,因此需要對(duì)上傳的大小進(jìn)行限制,以下為相關(guān)的三個(gè)參數(shù):
- client_max_body_size
- upload_max_filesize
- post_max_size
與以上相對(duì)應(yīng)的三個(gè)報(bào)錯(cuò)信息:
Warning: POST Content-Length of 9663102 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
$_FILES['file']['error']==1
nginx錯(cuò)誤:413 Request Entiry Too Large
client_max_body_size 用于設(shè)置客戶端 Request body(請(qǐng)求體)的大小上限,要上傳的文件就在 body 體 中,所以此參數(shù)可以間接的看做是對(duì)文件上傳大小的限制。
nginx 服務(wù)器通過請(qǐng)求頭的 Content-Length 確定 body 體的大小。超過設(shè)置的上限會(huì)返回錯(cuò)誤碼 413 Request Entity Too Large,將此參數(shù)設(shè)置為 0 可以取消對(duì)長(zhǎng)度的限制。
Syntax: client_max_body_size size;
Default:
client_max_body_size 1m;
Context: http, server, location
client_max_body_size 可以設(shè)置在 http、server、location 塊中,所以我們可以對(duì)域名甚至一個(gè)請(qǐng)求地址來提高上傳包的大小值。
php錯(cuò)誤:
Warning: POST Content-Length of 9663102 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
此時(shí)為上傳文件大小大于 post_max_size 。
php 無警告但是獲取不到上傳的文件
此時(shí) $_FILES['file']['error']==1
,錯(cuò)誤原因是上傳文件的大小小于 post_max_size 但是大于 upload_max_filesize 。
知識(shí)點(diǎn)開擴(kuò)展:
PHP和Nginx 文件上傳大小限制問題解決方法
對(duì)于nginx+php的一些網(wǎng)站,上傳文件大小會(huì)受到多個(gè)方面的限制,一個(gè)是nginx本身的限制,限制了客戶端上傳文件的大小,一個(gè)是php.ini文件中默認(rèn)了多個(gè)地方的設(shè)置。
所以為了解決上傳文件大小限定的問題必須要做出多處修改。以下整理了幾個(gè)地方。
1、修改/usr/local/nginx/conf/nginx.conf 文件,查找 client_max_body_size 將后面的值設(shè)置為你想設(shè)置的值。比如:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /home/www/htdocs; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/htdocs$fastcgi_script_name; include fastcgi_params; client_max_body_size 35m; #客戶端上傳文件大小設(shè)為35M client_body_temp_path /home/www/nginx_temp; #設(shè)置臨時(shí)目錄 }
附錄:Nginx有一個(gè)Upload組件:
上傳速率,上傳Body大小,也就是上傳文件時(shí)可能較大?
client_max_body_size 1024M
upload_limit_rate 158k
如下:
location /upload { upload_pass /up.php; upload_cleanup 400 404 499 500-505; #upload_store /data/app/test.local/upload_tmp; upload_store /tmp; upload_store_access user:r; client_max_body_size 1024M; upload_limit_rate 158k; upload_set_form_field "${upload_field_name}_name" $upload_file_name; upload_set_form_field "${upload_field_name}_content_type" $upload_content_type; upload_set_form_field "${upload_field_name}_path" $upload_tmp_path; upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5; upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size; upload_pass_form_field "^.*$"; #upload_pass_form_field "^pid$|^tags$|^categoryid$|^title$|^userid$|^user_id$|^is_original$|^upload_file_name$|^upload_file_content_type$|^upload_file_path$|^upload_file_md5$|^upload_file_size$"; }
2、修改php.ini
upload_max_filesize = 8M post_max_size = 10M memory_limit = 20M max_execution_time=300 file_uploads = On #默認(rèn)允許HTTP文件上傳,此選項(xiàng)不能設(shè)置為OFF。 upload_tmp_dir =/tmp/www
在上傳大文件時(shí),你會(huì)有上傳速度慢的感覺,當(dāng)超過一定的時(shí)間,會(huì)報(bào)腳本執(zhí)行超過30秒的錯(cuò)誤,這是因?yàn)樵趐hp.ini配置文件中 max_execution_time配置選項(xiàng)在作怪,其表示每個(gè)腳本最大允許執(zhí)行時(shí)間(秒),0 表示沒有限制。你可以適當(dāng)調(diào)整max_execution_time的值,不推薦設(shè)定為0。
總結(jié)
以上所述是小編給大家介紹的PHP 文件上傳限制問題,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

php使用mkdir創(chuàng)建多級(jí)目錄入門例子

WAMP環(huán)境中擴(kuò)展oracle函數(shù)庫(oci)

PHP設(shè)計(jì)模式(九)外觀模式Facade實(shí)例詳解【結(jié)構(gòu)型】

PHP生成指定長(zhǎng)度隨機(jī)數(shù)最簡(jiǎn)潔的方法

PHP實(shí)現(xiàn)登錄驗(yàn)證碼校驗(yàn)功能

ThinkPHP+EasyUI之ComboTree中的會(huì)計(jì)科目樹形菜單實(shí)現(xiàn)方法