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?


In reply to mod_perl file upload "end of file found" Or CGI.pm undefined filehandle by Jerzy2012

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.