简体   繁体   中英

How to limit file upload size in PHP?

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 (eg, 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM