Tanalis has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm currently using CGI::Application to provide some dynamic web content for a community website that I support.

As part of this, we want to allow users to upload an image for their personal profiles, with a maximum size of 80kb. This has been implemented, using the $CGI::POST_MAX variable to limit the size of uploads.

This seems to work well: if a file over the limit is uploaded, it's ignored, and the script returns (automatically, not through my scripting) to the site's homepage. What I'd like to do, however, is to catch the error, and display an error message, so that the user at least knows what went wrong.

I've looked through the docs for CGI, super-searched here and searched the web for some hints about how to do this, but I'm having no luck so far.

Does anyone know of a way to detect the fact that a file upload has exceeded the $CGI::POST_MAX limit, and hence failed, so that an error can be displayed? As the file doesn't actually get uploaded, I presume that the upload error-detection code given in the CGI module wouldn't work in this case.

Any suggestions would be appreciated.

-- Foxcub
#include www.liquidfusion.org.uk

Replies are listed 'Best First'.
Re: Detecting when a $CGI::POST_MAX limit is exceeded
by rob_au (Abbot) on May 26, 2003 at 10:01 UTC
    From Custom error handling for oversized CGI request entities ...

    #!/usr/bin/perl -Tw use CGI; use strict; BEGIN { my $POST_MAX = 512 * 1024; my $content_length = defined $ENV{'CONTENT_LENGTH'} ? $ENV{'CONTEN +T_LENGTH'} : 0; if ( ($POST_MAX > 0) && ($content_length > $POST_MAX) ) { # custom request entity too large error handling } }

    This code block uses the same logic employed by CGI for the handling of the oversize HTTP requests, however, whereas CGI returns a 413 error, this code block can be customised in its return output.

    BTW - CGI::Application rocks! :-)

     

    perl -le 'print+unpack"N",pack"B32","00000000000000000000001001011111"'

Re: Detecting when a $CGI::POST_MAX limit is exceeded
by benn (Vicar) on May 26, 2003 at 23:43 UTC
    from the CGI manual...
    An attempt to send a POST larger than $POST_MAX bytes will cause para +m() to return an empty CGI parameter list. You can test for this eve +nt by checking cgi_error()... <snip> ...If the POST was intercepted, then cgi_error() will return the messa +ge "413 POST too large".
    Cheese :),Ben.
Re: Detecting when a $CGI::POST_MAX limit is exceeded
by Hagbone (Monk) on May 26, 2003 at 21:54 UTC
    >Does anyone know of a way to detect the fact that a file upload has exceeded the $CGI::POST_MAX limit, and hence failed, so that an error can be displayed? As the file doesn't actually get uploaded, I presume that the upload error-detection code given in the CGI module wouldn't work in this case.

    I have an upload script that relies *solely* on the CGI.pm module, and the script incorporates a "maximum size" testing feature that has worked for me. Here's the snippet I use as the uploaded file is being "read in" during the upload:

    $max_size = '100000'; #set your max size value $totalbytes=0; open (IMAGESAVE,">$image_upload_directory/$final_name"); while ($bytesread=read ($file,$data,1024)) { $totalbytes+=$bytesread; if ($totalbytes > $max_size) { unlink ("$image_upload_directory/$file_name"); &FileTooLarge; #call your error sub exit; } print IMAGESAVE $data; } close IMAGESAVE;
    Given the level of knowledge at perlmonks, I may likely find out there's something wrong with the way I've got things set up ;), but as a said, the code above has worked for me for quite awhile.

      just realized .... $final_name should be $file_name in the file open line above.