問題描述
我應該如何以理智的方式處理超過 post_max_size
的 http 上傳?
How should I go about handling http uploads that exceeds the post_max_size
in a sane manner?
在我的配置中 post_max_size
比 upload_max_filesize
大幾 MB我遇到的問題是:
如果用戶上傳的文件超過 post_max_size
In my configuration post_max_size
is a few MB larger than upload_max_filesize
The problems I'm having are:
If a user uploads a file exceeding post_max_size
- _POST 數組為空
- _FILES 數組為空,當然其中不存在任何錯誤代碼.
- 沒有其他信息可以通過這些方式訪問什么樣的表單帖子.
部分問題在于接收腳本根據 POST 的內容采取不同的操作.
Part of the problem is that the receiving script takes different actions depending on the contents of the POST.
我確實可以訪問 _SERVER
變量并且可以獲得關于發生了什么的線索,即 CONTENT_TYPE
、CONTENT_LENGTH
和 REQUEST_METHOD代碼>.然而,根據這些內容進行猜測似乎很成問題.
I do have access to the _SERVER
variables and can get clues as to what happened, i.e. CONTENT_TYPE
, CONTENT_LENGTH
and REQUEST_METHOD
. It does however seem very problematic to make guesses based on those contents.
MEMORY_LIMIT(設置為相關大小的 10 倍)和 Apaches LimitRequestBody(設置為無限制)沒有問題.
MEMORY_LIMIT (set to 10 times the relevant sizes) and Apaches LimitRequestBody (set to unlimited) are found to not be at fault.
就目前而言,我什至很難向用戶提供任何有意義的信息.
As it stands now I have a hard time even providing any meaningful messages to the user.
有什么辦法可以保留一些表單數據,以便更好地了解發生了什么問題?我非常不愿意離開 php.
Is there any way to retain some form data to get better clues as to what has gone wrong? I'm very reluctant to move away from php.
推薦答案
對于不需要服務器端更改的簡單修復,我將使用 HTML5 文件 API 在上傳之前檢查文件的大小.如果超過已知限制,則取消上傳.我相信這樣的事情會奏效:
For a simple fix that would require no server side changes, I would use the HTML5 File API to check the size of the file before uploading. If it exceeds the known limit, then cancel the upload. I believe something like this would work:
function on_submit()
{
if (document.getElementById("upload").files[0].size > 666)
{
alert("File is too big.");
return false;
}
return true;
}
<form onsubmit="return on_submit()">
<input id="upload" type="file" />
</form>
顯然這只是一個例子的骨架,并不是每個瀏覽器都支持這個.但是使用它不會有什么壞處,因為它可以以這樣一種方式實現,它可以優雅地降級為舊瀏覽器的任何東西.
Obviously it's just a skeleton of an example, and not every browser supports this. But it wouldn't hurt to use this, as it could be implemented in such a way that it gracefully degrades into nothing for older browsers.
當然,這并不能解決問題,但它至少可以讓您的一些用戶以最少的努力感到滿意.(他們甚至不必等待上傳失敗.)
Of course this doesn't solve the issue, but it will at least keep a number of your users happy with minimal effort required. (And they won't even have to wait for the upload to fail.)
--
順便說一句,檢查 $_SERVER['CONTENT_LENGTH']
與帖子和文件數據的大小可能有助于檢測是否有問題.我認為當出現錯誤時它不會為零,而 $_POST
和 $_FILES
都是空的.
As an aside, checking $_SERVER['CONTENT_LENGTH']
vs the size of the post and file data might help detect if something failed. I think it when there is an error it will be non zero, while the $_POST
and $_FILES
would both be empty.
這篇關于如何檢測用戶上傳的文件是否大于 post_max_size?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!