Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count and limit the number of files uploaded (HTML file input)

Tags:

I have that basic, well known multiple file upload form. Something like that...

<input type="file" multiple="multiple" name="something[]" /> 

Is there a way (preferable a hard coded option) to limit the number of files that user can select? By limit i mean strictly blocked, not just a warning if number is exceeded.

Thanks in advance. :)

like image 911
Biker John Avatar asked Mar 21 '12 22:03

Biker John


People also ask

How do I restrict multiple file uploads in HTML?

To allow multiple file uploads in HTML forms, use the multiple attributes. The multiple attributes work with email and file input types. For limiting maximum items on multiple inputs, use JavaScript. Through this, limit the number of files to be uploaded.

How do you limit the maximum file selected when using multiple inputs?

Use two <input type=file> elements instead, without the multiple attribute. If the requirement is really just "have two different single inputs", then this is actually the best answer @Diego.

HOW include multiple files in HTML?

Tip: For <input type="file"> : To select multiple files, hold down the CTRL or SHIFT key while selecting. Tip: For <input type="email"> : Separate each email with a comma, like: [email protected], [email protected], [email protected] in the email field.

How can I count the number of files uploaded in PHP?

$countfiles = count(array_filter($_FILES['file']['name'])); It returns 0 in case of NULL, 1 in case of 1 file uploaded, etc.


Video Answer


1 Answers

You can implement a javascript solution to check the number of files that have been selected, which you can then use to forbid uploading to the server. There are really only going to be client-side solutions to this problem though, as there is really nothing stopping a user from posting these files to your php script anyway. You can specify a maximum upload size limit, but it isn't specific to the number of files that are being uploaded.

Your best bet is to implement some javascript checking, specifying a reasonable maximum upload size for your http server (or in PHP), and then ignoring any file uploaded if it exceeds your maximum count.

Read about the HTML5 File API here to restrict the count of selected files: http://dev.w3.org/2006/webapi/FileAPI/

Here is the php.ini docs which explain how to make a size limitation on uploads: http://php.net/manual/en/ini.core.php

As was suggested in the comments, check out: http://php.net/manual/en/ini.core.php#ini.max-file-uploads

like image 198
GoldenNewby Avatar answered Sep 19 '22 09:09

GoldenNewby