in reply to Limiting size of uploads

[id://Andre_br] let me just chime in with an observation, as I was deeply immersed in the internals of CGI.pm recently, and specifically the file upload portions of the code. Previous comment about using $CGI::POST_MAX is pretty correct, but the next reply about using $ENV{CONTENT_LENGTH} is far better in my opinion. What you need to know is that merely instanstiating a CGI object will cause the CGI module to read all of <STDIN> (which is what apache passes the post data to your script via) - what this means is: before you continue execution beyond that line you utter new CGI, the entire upload is written to a temp file on your disk. Therefore, preventing file uploads of a too-large-size is kind of useless after that happens, if you're trying to save on bandwidth. Saving on diskspace for storing that file, yes, but you'll still read and write the whole thing to disk before you even begin asking questions about it if the first thing you do is
my $q = new CGI;
so to save processing time and bandwidth and temp disk space, don't instantiate that until you're sure you want to. Also note, setting $CGI::POST_MAX will force CGI to stop reading from STDIN when it reaches that max, so any post variables that are in the stream AFTER the file upload (and HTTP defines no contraints as to what order variables ought be POSTed in, though in my experience most browsers POST in html form order) will not be exposed to you.

also note that
my $filehandle = $q->upload("file");
is a trivial function call, it's just handing back a file handle to the already existant file. I think this goes against most peoples expectations

my final words: use $ENV{CONTENT_LENGTH}, s'what it's there for. and CGI isn't as perfect as you think i thought it is was.

It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
     - Charles Wright & the Watts 103rd Street Rhythm Band

Replies are listed 'Best First'.
Re^2: Limiting size of uploads
by Andre_br (Pilgrim) on Jun 05, 2006 at 18:18 UTC
    Hello dear jZed, TedPride, monkfan and qbxk!

    I entered the code world a couple years ago, and while the language keeps proving itself one of the most powerfull available, the Perl community just couldn´t be more supporting! Happy to have you guys with me!

    I went with the most simple one, the -s monkfan suggested. Monkfan, this was just what I was looking for! Thanks!

    You guys also mentioned the $CGI::POST_MAX variable, wich is an extra security I have added. I wan´t bothering with users landing huge files on my disk, as long as I could get rid of them after checking the handle size. But it sure is a good thing to do.

    Also thanks a lot for the important info you provided on the CGI.pm internals, qbxk! Very interessting this detail, that the ->upload function is not the one triggering the upload, as I expected too. I'll keep that in mind now.

    Well, all implemented. Thanks a lot folks!

    André