PHP 上傳文件大小限制


修改 php.ini 文件

首先我們要修改php相關的一些配置來完成上傳文件時大小的限制。

file_uploads = on ;

是否允許通過HTTP上傳文件的開關。默認為ON即是開

upload_max_filesize = 8m ;

允許上傳文件大小的最大值。默認為2M

post_max_size = 8m ;

通過表單POST給PHP的所能接收的最大值,包括表單里的所有值。默認為8M

如果 POST 數據尺寸大於 post_max_size $_POST$_FILES superglobals 便會為空.

一般地,在網絡正常的情況下,設置好上述三個參數后,上傳<=8M的文件是不成問題。但如果要上傳>8M的大體積文件,只設置上述三項不一定能行的通。

max_execution_time = 30 ;

每個PHP頁面運行的最大時間值(秒),默認30秒。

超過30秒,該腳本就停止執行,這樣就會出現無法打開網頁的情況

max_input_time = 60 ;

每個PHP頁面接收數據所需的最大時間,默認60秒

memory_limit = 8m ;

每個PHP頁面所吃掉的最大內存,默認8M

修改 nginx.conf 文件

nginx對上傳文件大小是有限制的,解決方法是修補nginx相對應的配置文件。

client_max_body_size     50m; //文件大小限制,默認1m
client_header_timeout    1m; 
client_body_timeout      1m; 
proxy_connect_timeout     60s; 
proxy_read_timeout      1m; 
proxy_send_timeout      1m;

每個參數的意思:

client_max_body_size

限制請求體的大小,若超過所設定的大小,返回413錯誤。

client_header_timeout

讀取請求頭的超時時間,若超過所設定的大小,返回408錯誤。

client_body_timeout

讀取請求實體的超時時間,若超過所設定的大小,返回413錯誤。

proxy_connect_timeout

http請求無法立即被容器(tomcat, netty等)處理,被放在nginx的待處理池中等待被處理。此參數為等待的最長時間,默認為60秒,官方推薦最長不要超過75秒。

proxy_read_timeout

http請求被容器(tomcat, netty等)處理后,nginx會等待處理結果,也就是容器返回的response。此參數即為服務器響應時間,默認60秒。

proxy_send_timeout

http請求被服務器處理完后,把數據傳返回給Nginx的用時,默認60秒。

nginx錯誤:413 Request Entiry Too Large

client_max_body_size 用於設置客戶端 Request body(請求體)的大小上限,要上傳的文件就在 body 體 中,所以此參數可以間接的看做是對文件上傳大小的限制。

nginx 服務器通過請求頭的 Content-Length 確定 body 體的大小。超過設置的上限會返回錯誤碼 413 Request Entity Too Large,將此參數設置為 0 可以取消對長度的限制。

Syntax:    client_max_body_size size;
Default:   client_max_body_size 1m;
Context:    http, server, location

client_max_body_size 可以設置在 httpserverlocation 塊中,所以我們可以對域名甚至一個請求地址來提高上傳包的大小值。

修改/usr/local/nginx/conf/nginx.conf 文件,查找 client_max_body_size 將后面的值設置為你想設置的值。

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
      # 
      location ~ \.php$ { 
        root      /home/www/admin; 
        fastcgi_pass  127.0.0.1:9000; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME /home/www/admin$fastcgi_script_name; 
        include    fastcgi_params; 
   
        client_max_body_size 35m;    #客戶端上傳文件大小設為35M 
        client_body_temp_path /home/www/nginx_temp;    #設置臨時目錄 
      } 

附錄:Nginx有一個Upload組件:

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$";
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM