in reply to Detecting when a $CGI::POST_MAX limit is exceeded

>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.

Replies are listed 'Best First'.
Re: Re: Detecting when a $CGI::POST_MAX limit is exceeded
by Hagbone (Monk) on May 26, 2003 at 21:58 UTC
    just realized .... $final_name should be $file_name in the file open line above.