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

Fellow monks,
I am modifying a script to port it from *nix to IIs. All of the script is in good order as of today except for this file_upload sub. Can anyone advise me as to why I am getting the error: "Undefined subroutine CGI::upload" from this code?
TIA
jg
#!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw/fatalsToBrowser /; use CGI ':standard'; my $megabyte = 1024 * 1024; # bytes my $max_mb = 1; # max no. of MB we will allow $CGI::DISABLE_UPLOADS = 0; # CGI module variable for en/disabling uploads (non-zero to disable) $CGI::POST_MAX = $megabyte * $max_mb; # CGI module variable for maximum upload size (bytes) my $q = CGI->new(); my $base_dir = 'D:\\Inetpub\\thissite.org\\'; my $target_dir = "\\alerts\\images\\"; my $base_dom = "http://www.thissite.org/alerts"; my $image_folder="/images/";

The parsing code (not included) checks to see if the upload_file param is defined, and if it is it calls the sub below:

sub file_up { local $| = 1; my ($bytesread,$buffer,$file); my $directory = $base_dir . $target_dir; my $fh = $q->upload('upload_file'); binmode $fh; my $filename = $q->param('upload_file'); $filename =~ s/[^A-Za-z0-9\.\_]//g; open(OUTF, '>' . $directory . $filename); while ($bytesread = read($fh, $buffer, 1024)) { print(OUTF $buffer); } close(OUTF); if (!$file && $q->cgi_error) { print($q->header(-status=>$q->cgi_error)); exit 0; } $image_link = $base_dom . $image_folder . $filename; $image_link; }
_____________________________________________________
If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ

Replies are listed 'Best First'.
Re: Undefined subroutine CGI::upload
by chromatic (Archbishop) on Jan 07, 2002 at 02:28 UTC
    Because you're using a version of CGI older than 2.47?

    Update: Considering CGI.pm 2.42 is at least three years old, with multiple fixes to file uploads and to IIS interaction, and that most of the uploading examples take this into account, what do you think the proper solution is?

      Yeah, dws helped me get version number and it is 2.42
      Is there a workaround for this problem?
      TIA
      jg
      _____________________________________________________
      If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ
Re: Undefined subroutine CGI::upload
by BazB (Priest) on Jan 07, 2002 at 02:36 UTC

    I haven't got any ideas about exactly what is causing the problem, but I'll make a few suggestions, and there are a few things you can clean up.

    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/;

    You only need to do use CGI; once - decide which routine you want to load into your namespace - in this case standard.
    RTFM perldoc CGI for all the different routines that are available if standard doesn't do it for you.

    Be a little more consistent how you quote things - I've changed the code fragment above so that it's all quoted with slashes - it just makes it easier to read.

    Note the removal of the space from use CGI::Carp qw/fatalsToBrowser/; - just a small thing.

    As far as the problem itself goes - look at any lines that might have something to do with the problem.
    What happens if you alter or remove the CGI::POST_MAX and CGI::DISABLE_UPLOADS lines?

    Cheers

    BazB.

    Update: As suggested by chromatic, it seems that jerrygarciuh is using a rather old version of CGI.
    The solution is left as an exercise for the reader.