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

To anyone who can help the lowly: I am trying to have a simple upload script that is activated by a LWP script. The Web server is Apache 2 on Windows and the script looks like this:
use strict; use CGI; my $q=CGI->new(); my $client = $ENV{'REMOTE_ADDR'}; my $buffer; my $file = $q->param('upload_file'); $client =~ s/\./_/g; open(FH,"> c:/upload/$client.txt") or die($!); my $fh = $q->upload('upload_file'); while (<$fh>) { print FH; } close(FH); close(LOG);
This is the script that invokes the server script:
#!perl -w use LWP::UserAgent; use HTTP::Request::Common qw/POST/; my $ua= LWP::UserAgent->new(); my $filename= $ARGV[0]; chomp($filename); my $url='http://diesel.nci.nih.gov/cgi-bin/store.pl'; my %fields = ("upload_file" => "$filename"); my $results = $ua->post($url , content => [upload_file => "$filename"], enctype => 'multipart/form-data'); if($results->is_success){ print "It's good!\n"; } else { print "It didn't work.\n"; print $results->status_line(); }
The output from the web server is:
50:34 2004] [error] [client 156.40.134.176] Use of uninitialized value + in <HANDLE> at C:/Apache/scripts/store.pl line 19. [Wed Nov 10 13:50:34 2004] [error] [client 156.40.134.176] readline() +on unopened filehandle at C:/Apache/scripts/store.pl line 19.
The filename is create on the server but it is o bytes long. Does anyone have any ideas on why it isn't working? Any information is appreciated. tcox

Replies are listed 'Best First'.
Re: CGI Uploading Trouble
by howie (Sexton) on Nov 10, 2004 at 23:28 UTC
    Am I just being thick, or is your server-side script closing a filehandle that isn't mentioned anywhere else at all? (LOG). There isn't a line 19, but line 17 or so would be an uninitialized value, if that really is the whole of the script...
Re: CGI Uploading Trouble
by fglock (Vicar) on Nov 10, 2004 at 19:36 UTC
Re: CGI Uploading Trouble
by xorl (Deacon) on Nov 10, 2004 at 19:53 UTC
    On Windows, I think, you must set binmode() on any filehandle that will write out binary data. Google or "perldoc -f binmode" for more info.
Re: CGI Uploading Trouble
by steves (Curate) on Nov 10, 2004 at 20:23 UTC

    Here's a snippet from an upload script I use. I can't remember where I found the docs that explain what to set here though. I will look.

    $response = $ua->request(POST $url, Content_Type => 'form-data', Content => [ TextField1 => "TEST", mptest => +[$file]]);
    $file is just the name of the file but I seem to recall that it can also be a filehandle.

Re: CGI Uploading Trouble
by zentara (Cardinal) on Nov 11, 2004 at 14:17 UTC
    Here is some code that works in my local testing:
    #!/usr/bin/perl use warnings; use strict; use HTTP::Request::Common qw(POST); use LWP::UserAgent; my $url = 'http://zentara.zentara.net/~zentara/cgi-bin/up1a.cgi'; my $file = 'testout.tgz'; my $filezzzz = 'testzzzz.tgz'; my $ua = new LWP::UserAgent; my $req = POST $url, Content_Type => 'multipart/form-data', Content => [ p_upload => [ $file, $filezzzz ] #actual file, name to use as u +pload ]; my $res = $ua->request($req); if ( $res->is_success ) { print $res->as_string; } else { print $res->status_line; } exit 0;

    I'm not really a human, but I play one on earth. flash japh