wertert has asked for the wisdom of the Perl Monks concerning the following question:
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.
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.html****************** 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;
***************** uper.pl ? DOESN?T WORK ? run on Win2000.<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> </p></body> </html>
#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: LWP+cgi file upload problems
by chromatic (Archbishop) on Jul 14, 2001 at 08:21 UTC | |
|
Re: LWP+cgi file upload problems
by wertert (Sexton) on Jul 11, 2001 at 01:19 UTC |