ok so I've spent the last 2 days in the twilight zone. I cannot get something straight-forward to work. Please help.

I want to upload files to a web site using a script. The site has a html + CGI script that are able to successfully upload a file when using a web browser. Give it a try on http://www.perfectotion.co.uk/upload4s.html.

I am trying to get the SAME thing working using the LWP::UserAgent;. There are plenty of examples about and I?ve closely followed an example from lwpcook.

The client I?m running on is win2000 + Activeperl5.6. The server is a commercial server running solaris+apache.

I?ve given the various scripts below. I need to explain one thing that is a separate problem with the upload4s.cgi script.

PROBLEM(1)I couldn?t get the traditional upload script to work i.e
my $file = $q->param("file") || error ($q, " No file received."); my $filename = $q->param("filename") || error ($q, " No filename enter +ed."); my $fh = $q->upload( $file ); $tmp="../user/$filename"; open(OUTPUT, ">$tmp") or die ("Cannot open upload file"); while ( read( $fh, $buffer, 1024) ) { print OUTPUT $buffer; }

what seems to happen is the temp file opened by $fh receives the data but the while loop never does anything so ../user/$filename is created but zero length.

The workaround is to access the temp file directly. I?ve used a call to unsupported CGI.pm method tmpFileName() to give me the tmp file name. I then read this and write out to the final output file. Seems to work well but only from a browser dialog. No joy with LWP::UserAgent. It would seem that I?m not formatting the file post from uper.pl but I cannot find out what?s wrong. The theory is that if it works from a browser it should work from the LWP script provided it's formatted properly.

So here are the various scripts
****************** Upload4s.cgi #!/usr/bin/perl -- $|=1; #Switch off buffering. use CGI qw( :standard ); use Fcntl qw( :DEFAULT :flock); use constant UPLOAD_DIR => '../user'; use constant BUFFER_SIZE => 16_384; use constant MAX_FILE_SIZE => 1_048_576; use constant MAX_DIR_SIZE => 100 * 1_048_576; use constant MAX_OPEN_TRIES => 2; $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = MAX_FILE_SIZE; my $q = new CGI; print $q->header, $q->start_html, $q->Dump; $q->cgi_error and error( $q, "Error transferring file: " . $q->cgi_err +or ); my $file = $q->param("file") || error ($q, " No file received."); my $filename = $q->param("filename") || error ($q, " No filename enter +ed."); my $buffer = ""; $tmp="../user/$filename"; open(OUTPUT, ">$tmp") or die ("Cannot open upload file"); binmode OUTPUT; open(FH,$q->tmpFileName($file)) or die ("eeeek dead"); while (read( FH, $buffer, 1024) ) { #FH print OUTPUT $buffer; } close OUTPUT; close FH;
**************** upload4s.html
<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +"> </head> <body bgcolor="#FFFFFF"> <form action="upload4s.cgi" method="POST" enctype="multipart/form-data +"> <p>Please choose a file to upload: <input type="FILE" name="file"> <p>Please enter the name of this file: <input type="TEXT" name="filename"> </form> <p>&nbsp;</p></body> </html>
***************** uper.pl ? DOESN?T WORK ? run on Win2000.
#!/perl/bin/perl use HTTP::Request::Common qw(POST); use LWP::UserAgent; use LWP::Debug qw(+); my $ua = new LWP::UserAgent; my $req = POST 'http://www.perfectmotion.co.uk/upload4s.cgi', [ filename => '111', file => 'C:\test.bat? ]; # print $req; my $res = $ua->request($req); print $res->as_string;

In reply to LWP+cgi file upload problems by wertert

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.