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

I have recently ported most of my CGI programs to mod_perl. I _need_ Apache.pm loaded, and those of you familiar with mod_perl unterstand the importance of removing redundant code. Everything was going great, until I needed to reuse some old code to accept user uploads. The only code uses CGI.pm, and I don't want to do that. My question is, how do I get at the uploaded file via an apache request object, and how is this different (internally) than with CGI.pm?

Here is the code that is not working

my %params = $r->method eq 'POST' ? $r->content : $r->args; while ( my ( $key, $value ) = each %params ) { if ( "some reg-exp that is true if there is an upload" ) { print STDERR "value=$value\n"; my $tempfile = $value; #some fiddling with the $tempfile my $buffer; print STDERR "tempfile=$tempfile\n"; open( *OUTFILE,">>/tmp/myb.$tempfile") || die "couldn't op +en outfile: $!"; while ( read( $value, $buffer, 1024 ) ) { print OUTFILE $buffer; } close( *OUTFILE ); } }

and $tempfile gets created, but is empty.

I've tried perdoc Apache, and I've tried the mod_perl Guide.

Any help or references would be greatly appriciated.

Replies are listed 'Best First'.
Re: uploads with Apache.pm
by btrott (Parson) on Jul 12, 2000 at 09:42 UTC
    You want Apache::Request, part of libapreq.

    From the docs:

    upload Returns a single Apache::Upload object in a scalar context or all Apache::Upload objects in an array context: my $upload = $apr->upload; my $fh = $upload->fh; my $lines = 0; while(<$fh>) { ++$lines; ... } An optional name parameter can be passed to return the Apache::Upload object associated with the given name: my $upload = $apr->upload($name);
      Excellent, thank you! That's an odd place for them to hide it, I am surprised it is not standard. Particularly confusing was that I was already using Apache->request(); # the namespace seems cluttered.
      Paris Sinclair    |    4a75737420416e6f74686572
      pariss@efn.org    |    205065726c204861636b6572
      I wear my Geek Code on my finger.
      
Re: uploads with Apache.pm
by Aighearach (Initiate) on Jul 12, 2000 at 09:09 UTC
    I guess I should add that $r is an Apache request object created with
    my $r = shift || Apache->request();
    Paris Sinclair    |    4a75737420416e6f74686572
    pariss@efn.org    |    205065726c204861636b6572
    I wear my Geek Code on my finger.