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

I've some questions regarding the usage of CGI::Application. I have a run-mode which handles file uploading. How can I set the $CGI::POST_MAX variable to limit the size of uploaded files? I will also want to dynamically set $CGI::DISABLE_UPLOADS if fiel uploading is disabled for some reason. Also, what is the easiest method to determine the MIME type of uploaded files? Right now I'm using the MIME::Types::by_suffix routine. Thanks for your help. P.S. I forgot to ask, When finished with the upload run-mode, I'd like to go to my default mode, which just displays a form. Right now I'm redirecting to the url. Is there another way? Perhaps to directly call the subroutine associated with that run leve?

2005-11-09 Retitled by GrandFather from CGI::Application usage
Original title: 'CGI::Application usage'

  • Comment on Setting CGI::POST_MAX with CGI::Application

Replies are listed 'Best First'.
Re: Setting CGI::POST_MAX with CGI::Application
by mlh2003 (Scribe) on Nov 09, 2005 at 13:03 UTC
    You can explictly set $CGI::POST_MAX to some value in your instance script. You will need to pass the query object via the new() method so that it is inherited by your application module.
    For example, your instance script may look like:
    #!/usr/bin/perl -T use strict; use warnings; use lib '/home/lib'; use Current::My_App; use CGI; $CGI::POST_MAX=102400; # Limit post to 100kB $CGI::DISABLE_UPLOADS=0; # Allow file uploads my $q=CGI->new; # Create query object if ($q->cgi_error()) { print $q->header(-status=>$q->cgi_error()); exit 0; } my $app=Current::My_App->new(query=>$q); $My_App->run();

    As for determining the MIME type of a file, the File::Type module might be useful for your needs.
    _______
    Code is untested unless explicitly stated
    mlh2003

      Wouldn't overriding CGI::Application's cgiapp_get_query method be cleaner?

      untested:

      sub cgiapp_get_query { my $self = shift; # Include CGI.pm and related modules require CGI; $CGI::POST_MAX=102400; $CGI::DISABLE_UPLOADS=0; # Get the query object my $q = CGI->new(); return $q; }
      -derby
Re: Setting CGI::POST_MAX with CGI::Application
by friedo (Prior) on Nov 09, 2005 at 13:41 UTC
    To send the user back to your form runmode, instead of using a redirect, you can just call the method directly, like this:

    sub upload_file { my $self = shift; # handle file upload.... return $self->form_mode; }
      And what happens now when the user hits Reload? These should always be redirects.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        DC, I'm not following. The call to the default run mode should set the correct runmode param as a hidden var in the html ... so a reload should just reload the default form ... Am I missing something?
        -derby