如何設定PHP上傳檔案大小限制

來源:互聯網
上載者:User

一,如果php沒有限制我們可以在上傳時就限制上傳大小,如

 

 代碼如下 複製代碼
$maxattachsize=5097152;//最大上傳大小,預設是2m
$temppath=$upfile['tmp_name'];
$filesize=filesize($temppath);
if($filesize > $maxattachsize)$err='檔案大小超過'.$maxattachsize.'位元組';

這樣就限制了檔案只能上傳2MB大小的檔案了


二,修改php.ini中的post_max_size和upload_max_filesize

1、memory_limit記憶體設定限制

2、max_execution_time程式執行時間限制

3、post_max_size最大POST資料限制

4、upload_max_filesize最大上傳檔案大小限制

 代碼如下 複製代碼

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60    ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M      ; Maximum amount of memory a script may consume (128MB)

; Maximum size of POST data that PHP will accept.
post_max_size = 105M

; Maximum allowed size for uploaded files.
upload_max_filesize = 100M

小提示,如果你是上傳大檔案我們最好在頁面加上執行時間set_time_limit

 代碼如下 複製代碼

set_time_limit(0);

這樣就不會逾時了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.