How do I limit file size on <input type=file...>?

D

Don

How do I limit the uploaded file size on <input type=file...>? I believe the "size" attribute
controls the size of the input box, and "maxlength" limits the number of chars that can be entered
in that input box. But, what attribute sets a limit on the size of the actual uploaded file?

Thanks for any help.
Don
 
M

mbstevens

Don said:
How do I limit the uploaded file size on <input
type=file...>? I believe the "size" attribute controls
the size of the input box, and "maxlength" limits the
number of chars that can be entered
in that input box. But, what attribute sets a limit on
the size of the actual uploaded file?

Server side, it can be done
with a loop that reads in chunks, testing with each chunk
whether the size limit has been reached.

There may be a module/library in your server side language
that simplifies this, but it's not a complex task. Be
sure to include code that handles the error nicely if the
file is too large.
 
H

Hywel Jenkins

How do I limit the uploaded file size on <input type=file...>? I believe the "size" attribute
controls the size of the input box, and "maxlength" limits the number of chars that can be entered
in that input box. But, what attribute sets a limit on the size of the actual uploaded file?

None.
 
D

Don

Server side, it can be done
with a loop that reads in chunks, testing with each chunk
whether the size limit has been reached.

There may be a module/library in your server side language
that simplifies this, but it's not a complex task. Be
sure to include code that handles the error nicely if the
file is too large.
Thanks for your reply. How do I read the file in chuncks and put it back together into one
contiguous file?

Don
 
H

hyweljenkins

mbstevens said:
Server side, it can be done
with a loop that reads in chunks, testing with each chunk
whether the size limit has been reached.

How do you access the file *while* it's being uploaded to test those
"chunks"? The file data gets sent with the rest of the POST data, so
it will arrive at the upload script in one piece, won't it?
 
M

mbstevens

How do you access the file *while* it's being uploaded
to test those
"chunks"? The file data gets sent with the rest of the
POST data, so it will arrive at the upload script in one
piece, won't it?
No. You usually set the form to
METHOD="post"
ENCTYPE="multipart/form-data"

This is a loop that does it in Perl
(omitting the rest of the program, natch):

while ($chunk = read ($file, $data, 1024)) {
$length += $chunk;
if ($length >= $MAX_LENGTH){
process_your_errors();
}
}
 
M

mbstevens

Don said:
Thanks for your reply. How do I read the file in
chuncks and put it back together into one contiguous
file?

See my reply to Hewel below in the thread. The data gets
temporarily stored in the $data variable, which can later
be written to file. Any server side language should be
able to do the same thing -- I'm just particularly fond
of Perl.
 
M

mbstevens

mbstevens said:
No. You usually set the form to
METHOD="post"
ENCTYPE="multipart/form-data"

This is a loop that does it in Perl
(omitting the rest of the program, natch):
I should add that you will have a file open...
while ($chunk = read ($file, $data, 1024)) {
$length += $chunk;
print ANOPENFILE $data;
# which is where the data gets written
if ($length >= $MAX_LENGTH){
process_your_errors(); exit;
}
}
......and then close the file
 
D

Don

See my reply to Hewel below in the thread. The data gets
temporarily stored in the $data variable, which can later
be written to file. Any server side language should be
able to do the same thing -- I'm just particularly fond
of Perl.
Thanks much. I read your note below. I'll give it a try. Really appreciate your help.
Regards,
Don
 
K

kchayka

mbstevens said:
This is a loop that does it in Perl

while ($chunk = read ($file, $data, 1024)) {
$length += $chunk;
if ($length >= $MAX_LENGTH){
process_your_errors();
}
}

FYI the perl CGI.pm module has this built in:

use CGI;
$CGI::pOST_MAX = 20000; (or whatever size you want)

It will show a standard error message if the size exceeds this, or you
can send errors to a function that prints a "pretty" message if you
prefer. I've used it on mail forms before, works like a champ.
 
M

mbstevens

kchayka said:
FYI the perl CGI.pm module has this built in:

use CGI;
$CGI::pOST_MAX = 20000; (or whatever size you want)

It will show a standard error message if the size
exceeds this, or you can send errors to a function that
prints a "pretty" message if you prefer. I've used it on
mail forms before, works like a champ.

I think you're mostly right. On the 'average' form
depending on POST_MAX should work.
I always use CGI::pOST_MAX in my own scripts.

I didn't use it in this answer because POST_MAX may be
greater than the size you want to allow for a particular
file -- POST_MAX is for the max bytes you allow each form
to submit through uploaded files *and* through regular
fields, if I understand it correctly.
 
D

Don

Server side, it can be done
with a loop that reads in chunks, testing with each chunk
whether the size limit has been reached.

There may be a module/library in your server side language
that simplifies this, but it's not a complex task. Be
sure to include code that handles the error nicely if the
file is too large.
Thanks everyone. I appreciate alll your help.

Regards,
Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top