Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict file upload size limit based on file type in .htaccess

Using .htaccess, is it possible to restrict the php file upload size limit based on type of file ?

For example, the maximum upload limit for images is 2 MB and maximum upload limit for PDF and DOC is 10 MB.

like image 588
Rajesh Ashok Kumar Avatar asked Dec 19 '12 15:12

Rajesh Ashok Kumar


People also ask

How can you limit upload size by users?

You can make use of the LimitRequestBody directive to limit upload file size. The value assigned to the LimitRequestBody allows Apache to accept and store file uploads of 9000 bytes by users.


1 Answers

I would use the LimitRequestBody directive for this. However, to quote:

Implementations of the PUT method will require a value at least as large as any representation that the server wishes to accept for that resource.

You can set this to 10Mb to have Apache cut off any request which is surely too large, and then further test and filter within your script. This directive is mostly used to prevent denial-of-service attacks. Without this directive, Apache will gladly accept the entire file before it ever reaches your script, so users could be uploading 100Gb files before being told that it's too long.

This directive will not peek into the body of the request, though, so you can't filter on file type with it.

like image 167
Ben Deutsch Avatar answered Oct 13 '22 09:10

Ben Deutsch