問(wèn)題描述
我想知道名為 MAX_FILE_SIZE
的隱藏字段應(yīng)該如何工作?
I am wondering how is the hidden field named MAX_FILE_SIZE
supposed to work?
<form action="" method="post" enctype="multipart/form-data">
<!-- in byes must preceed file field -->
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
<input type="file" name="upload" />
<input type="submit" name="submit" value="Submit" />
</form>
我上傳了一個(gè) 4MB 以上的文件,但我沒(méi)有收到來(lái)自客戶(hù)端的警告(我不是在談?wù)摲?wù)器端).MAX_FILE_SIZE
應(yīng)該做什么?
I uploaded a 4MB+ file but I got no warning from client side (I am not talking about server side). What is it MAX_FILE_SIZE
supposed to do?
更新
好的,所以 PHP 強(qiáng)加了一個(gè)軟"限制.但是使用它和檢查諸如 $_FILES['upload']['size'] < 之類(lèi)的東西有什么區(qū)別嗎?2000
在代碼中?
OK so its for PHP to impose a "soft" limit. But is there any difference between using it and checking something like $_FILES['upload']['size'] < 2000
in code?
推薦答案
MAX_FILE_SIZE
是 KB 而不是 字節(jié).你是對(duì)的,它以字節(jié)為單位.因此,對(duì)于 4MB 以字節(jié)為單位轉(zhuǎn)換 4MB {1024 * (1024 * 4)}
的限制,請(qǐng)嘗試:
MAX_FILE_SIZE
is in KB not bytes. You were right, it is in bytes. So, for a limit of 4MB convert 4MB in bytes {1024 * (1024 * 4)}
try:
<input type="hidden" name="MAX_FILE_SIZE" value="4194304" />
正如其他人所解釋的那樣,您永遠(yuǎn)不會(huì)收到警告.它只是在服務(wù)器端強(qiáng)加一個(gè)軟限制.
As explained by others, you will never get a warning for this. It's there just to impose a soft limit on server side.
回答您的子問(wèn)題.是的,有區(qū)別,您永遠(yuǎn)不要相信用戶(hù)輸入.如果您想始終施加限制,則必須始終檢查其大小.不要相信 MAX_FILE_SIZE
的作用,因?yàn)樗梢杂捎脩?hù)更改.所以,是的,您應(yīng)該檢查以確保它始終達(dá)到或超過(guò)您想要的尺寸.
To answer your sub-question. Yes, there is a difference, you NEVER trust the user input. If you want to always impose a limit, you always must check its size. Don't trust what MAX_FILE_SIZE
does, because it can be changed by a user. So, yes, you should check to make sure it's always up to or above the size you want it to be.
不同之處在于,如果您將 MAX_FILE_SIZE
強(qiáng)加為 2MB,并且用戶(hù)嘗試上傳 4MB 的文件,一旦達(dá)到大約上傳的前 2MB,傳輸將終止,PHP 將停止接受該文件的更多數(shù)據(jù).它將報(bào)告文件數(shù)組的錯(cuò)誤.
The difference is that if you have imposed a MAX_FILE_SIZE
of 2MB and the user tries to upload a 4MB file, once they reach roughly the first 2MB of upload, the transfer will terminate and the PHP will stop accepting more data for that file. It will report the error on the files array.
這篇關(guān)于HTML 上傳 MAX_FILE_SIZE 似乎不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!