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

Hey,

I'm still having some problems that I can't seem to figure out. I'm concerned that I'm not even on the right track and I'm using CGI.pm totally wrong? I'm on a Windows machine.

This script uses Data::FormValidator along with CGI::Application::ValidateRM to validate the form input. The beginning part of the script is mostly the form_display and form_process to dispaly and validate the form, after is the upload portion where I've been stuck on since early mourning. I recently posted a thread on setting the size restriction for uploaded files. I seem to have that working in a way.

Heres my situation. Lets say I set $CGI::POST_MAX = 1536000 to $CGI::POST_MAX = 20. When trying to submit the form, it'll return a blank template (upload.tmpl). Somehow I'm getting a sense that the POST_MAX is working considering, when I change it back to 153600 and try to submit the form, the whole process works--Including uploading the file.

My main problem is retrieving the error message if a file being uploaded surpasses my limit, which in my case, 1.7mb. I used comments in the upload process portion of the script to describe what Is wrong and where I'm having trouble on,so I don't think you really need to focus much on this script but the results and upload portion.

Here's my following code:

package BusinessFormValidation; use base CGI::Application; use CGI::Application::ValidateRM; use Data::FormValidator; use CGI; $CGI::POST_MAX = 1536000; #$CGI::POST_MAX = 1536000; #~1.7MB use strict; print "Content-type: text/html\n\n "; sub setup { my $self = shift; $self->start_mode('form_display'); $self->run_modes([qw/ form_display form_process # data_process # upload_process /]); } sub form_display { my $self = shift; my $errs = shift; my $t = $self->load_tmpl('upload.tmpl',die_on_bad_params=>0); $t->param($errs) if ref $errs; return $t->output; } sub form_process { my $self = shift; my ($results,$err_page) = $self->check_rm('form_display', { required => [qw/ industry firstname lastname company address1 city us_states country zip_code ci_month ci_year p1 p2 p3 /], optional => [qw/ title province address2 f1 f2 f3 website zip_codex /], # dependencies => { # name1 => [ '2_name'], # }, constraints => { zip_code => 'zip', ci_email => 'email', }, # trim leading and trailing whitespace from all the val +id fields. filters => ['trim'], field_filters => { cc_no => "digit", zip_code => "digit", zip_codex => "digit", p1 => "digit", p2 => "digit", p3 => "digit", f1 => "digit", f2 => "digit", f3 => "digit", }, msgs=>{ any_errors => 'err__', prefix=>'err_', format => ' <b><font color="white" face="verdana" size +="1">*</font><font face="verdana" size="1">%s</font></b>', }, }); return $err_page if $err_page; #basic form elements are valid , upload validation/process: #foreach my $f ( $results->valid() ) { # print $f, " = ", $results->valid( $f ), "\n"; # } #prints out the form results. my $upload_dir = "C:/web site/uploads"; my $business_plan_file = $results->valid('file_name'); #upload f +iled name is file_name $business_plan_file =~ s/.*[\/\\](.*)/$1/; #retrieve filename +w/out full path my $upload_filehandle = $results->valid('file_name'); ## #my $content_type = $upload_filehandle->uploadInfo($business_plan_file +)->{'Content-Type'}; #print "hi"; if I uncomment this and the line above, the script will + somehow seem to die and will not print "hi" ## my $error = $upload_filehandle->error(); #this is where my main problem is being caused. Somehow its not retri +eving the error messages if the POST surpasses 1.7 mb. if ($error) { print "$error"; die; } #elsif { #my $content_type = $cgi->uploadInfo($business_plan_file)->{'Content-T +ype'}; #this will be placed if it passes the POST requirement length, then wi +ll check for specific MIME types allowed #} #this is the simple upload script I'm using, does this involve anythin +g whatsoever with the CGI.pm module? It seems like this code could +just be ran w/out "use CGI.pm" else { open UPLOADFILE, ">$upload_dir/$business_plan_file" || die; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; } } 1;


I'm just not sure about this problem. It seems like I'm on the right track with doing this but probably missing something that I'm not aware of. I checked the docs etc and still no luck when trying to play with it. Am I even on the right track with what I want to do? Which is validate a form, validate a upload, upload the acutal file, and then the rest is to insert the data into a mysql table-which I'm sure I won't have trouble with once I get the upload process done.

Thanks,
Anthony

Replies are listed 'Best First'.
Re: My Upload script, Am I even on the right track?
by tachyon (Chancellor) on Mar 18, 2004 at 00:54 UTC

    If you exceed POST_MAX CGI.pm flags it by setting cgi_error as follows....

    $self->cgi_error("413 Request entity too large");

    Just check the error messages using the cgi_error() method and the solution is to hand. Could I subtly hint that you read up about indentation. Settle on any style but aim for consistency. I find your code somewhat painful to read and harder to follow that it would be if it was formated in a less haphazard way. Have a look a perltidy and its output for some suggestions.

    Now to be pedantic 1MB is 1024*1024 bytes so 1.7MB is 1.7*1024*1024 == 1782759 Bytes. This is nothing like 1536000 bytes. It is 1972633.6 bits different :-) When I read rather inaccurate comments like that it........

    cheers

    tachyon