http://www.perlmonks.org?node_id=1005944

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

Hello fellow Perl coders.

I spent half a day looking for a solution and didn't find a working one. I would appreciate any help.


I am trying to do a simple web based file upload script. It's a part of bigger project so I am using mod_perl (ModPerl::Registry)

In my main class named Main.pm I have following code:
my $cgi = CGI->new; my $self = { OBJ_cgi => $cgi };

In my module File.pm I have:

my $r = Apache2::RequestUtil->request; my $req = Apache2::Request->new($r); my @upload = eval { $req->upload() }; print STDERR Dumper($@);
This gives me:
NAMES: $VAR1 = bless( { 'rc' => 70014, '_r' => bless( do{\(my $o = '140573114765624')}, 'Apa +che2::Request' ), 'file' => '/usr/lib/perl5/APR/Request.pm', 'func' => 'APR::Request::body', 'line' => 50 }, 'APR::Request::Error' );
If I execute it without eval, it throws following code in apache error log file:
[error] End of file found

My HTML form:

<form action="uploaddocument.cgi" method="POST" enctype="multi +part/form-data"> <input type="file" name="file_content"> <input type="hidden" name="id" value="2"> <input type="hidden" name="save" value="1"> <input type="submit" value="Upload file"> </form>

I checked it on two browsers(chrome, firefox). I checked this code on two different servers(Debian and CentOS). It doesn't work anywhere.

I tried to use a CGI.pm way of handling files:

my $fh = $self->{OBJ_cgi}->upload('file_content'); print STDERR Dumper($fh);

And this prints undef to STDERR

The interesting part is that it correctly reads the name of file if I use param method instead of upload. Also during the script execution the file is correctly saved in /var/tmp/CGItemp...

I would be extremely grateful for any help.
Best regards,
Jerzy

UPDATE

I managed to get this thing working. In Main.pm in method new - first method that is executed at all - I added:
my $r = Apache2::RequestUtil->request; my $req = Apache2::Request->new($r);

I don't do anything with these objects. I don't pass those anywhere. I can even undef both of those right after declaring it. So my question changes to: Why this works after adding those 2 lines?