开发者

How to limit file upload size in PHP?

开发者 https://www.devze.com 2023-04-12 01:29 出处:网络
I want to save bandwidth and restrict the size of uploaded files to our webpage. The problem is that I can set the maximal size of uploaded file (e.g., 1 MB) but a user can select 50 MB file and can u

I want to save bandwidth and restrict the size of uploaded files to our webpage. The problem is that I can set the maximal size of uploaded file (e.g., 1 MB) but a user can select 50 MB file and can upload it to find out that PHP really doesn't accept files of this size -- the uploading is totally unnecessary.

Can I prevent this somehow?

(I can check file size in Javascript but not all browsers support this. Flash is an option but it's not universal either).

开发者_StackOverflow社区Thanks!


Use the LimitRequestBody Directive in .htaccess:

Example (setting a 1Mb limit). Create/modify your .htaccess file (in your webroot, or higher), and add:

LimitRequestBody 1048576

When the limit is exceeded, the request will be interrupted. The user will get a response similar to:

413 Request entity too large


Also: Rule 1: Never trust the client! You should always do such checks server-side, because the client can easily circumvent your JavaScript checks, and do anything which is not restricted by the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号