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

I have read many nodes regarding file upload. I use 'strict' as much as possible.

I thought that there would be no problem integrating LWP on the client side and CGI on the server side but this does not seem to be the case.

I am using a call like this on the client:

my $enctype = 'form-data/multipart'; $ua = LWP::UserAgent->new; $req = POST $url, Content_Type => $enctype, Content => [ p_progra_idint =>$p_progra_idint, p_upload => [$path_file,$name_file] ]; $res = $ua->request($req);

And on the server side:

$CGI::DISABLE_UPLOADS = 0; $ENV{'TMPDIR'} = $tpath; binmode(STDIN); $req = new CGI(\*STDIN); my $fh = $req->upload('p_upload'); my $buffer; open (OUTFILE, ">$path_sortida") or die "Unable to create upload output file $! \n"; binmode $fh; binmode OUTFILE; my ($bytes, $read); $bytes = 0; while ($read = read ($fh, $buffer, 16384)) { print OUTFILE $buffer; $bytes += $read; } close OUTFILE;
Strangely, if I use 'form-data/multipart' all variables except the file are read perfectly on the server side, on the other hand if I use 'multipart/form-data' (which is what it should be) all sorts of erroneous variables are read.

With the first option as encoding, all vars are read, but 0 bytes of the file are returned after the bucle. The file is created but with zero size.

I have browsed the perlmonks archives and tutorials as well as done some obvious google querys, now I am lost and I am thinking on doing the parsing by hand on the server (ugh).

Thanks in advance.

Replies are listed 'Best First'.
Re: Yet another file-upload question (using LPW--->CGI)
by amphiplex (Monk) on Jul 17, 2002 at 10:40 UTC
    Hi !

    Maybe I am missing something here, but your scripts worked for me when I removed the
    (\*STDIN)
    Why did you do that ?

    The encoding type is definitely
    multipart/form-data

    ---- amphiplex
      YAY! I did just that as well.

      'Yet Another Stupid Mistake(tm)' on my part.

      Thanks a lot though.

Re: Yet another file-upload question (using LPW--->CGI)
by dani++ (Sexton) on Jul 17, 2002 at 10:11 UTC
    (Update):

    Uh, using 'form-data/multipart' sends the query without the binary. However, using the correct 'multipart/form-data' sends a correct looking query (with the binary) which confuses 'CGI.pm' big time.

    Saving the received query 'as is' and doing a 'vi' on it shows that all the end of lines are in binary (^M in vi) but CGI does not seem to recognize them, the bastard.

    ...