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

I am writing a little utility that will put a result file on a web server after running. I am using LWP and my 2 programs are:
#!perl # Store.pl: sits on a web server waiting to store a file. use CGI; open(LOG,">upload.log"); my $q=CGI->new(); $q->header(); $q->start_html(); my $client = $ENV{'REMOTE_ADDR'}; print LOG "Client: $client\n"; $file = $q->param("upload_file"); print LOG "File: $file\n"; $client =~ s/\./_/g; open(FH,"> c:/upload/$client.txt") or die($!); binmode FH; my $fh = $q->upload($file) or print LOG "Error uploading file $!\n"; print $fh; while (<$fh>) { print FH $_; } $q->end_html(); close(FH); close(LOG);
And the client file is:
use LWP::UserAgent; $URL_Server = $ARGV[0]; $fn = $ARGV[1]; my $ua = LWP::UserAgent->new(); my %fields = (upload_file => $fn); my $res = $ua->post("http://$URL_Server/cgi-bin/store.pl", \%fields, ' +Content_Type' => 'multipart/form-data'); print $res->as_string;
On the web server the file is created but no data is written. The result message is "500 error" and the error log just says "premature end of script headers." Does anyone have any thoughts?

Replies are listed 'Best First'.
Re: LWP file upload question
by davorg (Chancellor) on Aug 18, 2004 at 13:59 UTC

    I think you should look in the web server error log and see what the problem is.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: LWP file upload question
by iburrell (Chaplain) on Aug 18, 2004 at 16:15 UTC
    When you a 500 error and "premature end of script headers", that usually means the script failed to run. Most likely because it couldn't compile because of syntax errors. Also, happens when perl is not running properly, or with line endings on the scripts. Look in the error log. If there is no message, try running the script from the command line and fix the errors. Add "use strict;" to the top and fix all those errors.

    Also, upload takes the parameter name, not the filename.

    my $file = $q->param('upload_file'); my $fh = $q->upload('upload_file');
Re: LWP file upload question
by reneeb (Chaplain) on Aug 18, 2004 at 17:08 UTC
    To get a more detailed error message use use CGI::Carp qw(fatalsToBrowser);
    in your script. This prints error messages through STDERR!
Re: LWP file upload question
by Plankton (Vicar) on Aug 18, 2004 at 14:18 UTC
    You say, "On the web server the file is created but no data is written." ... which file? upload.log? c:\upload\$client.txt? Or $file? It looks like you don't close the filehandle to $file ... Just a guess.

    Plankton: 1% Evil, 99% Hot Gas.
      The c:\upload\$client.txt file is created but no data is written. I have the close(FH) at the end which is the c:\upload\$client.txt filehandle.
      The upload.log file is written and saved without any problems. In response to the first reply the error.log file says "Premature end of script headers."
Re: LWP file upload question
by Plankton (Vicar) on Aug 18, 2004 at 15:00 UTC
    Well here's some more bad advice ... The only thing I can think of is that it is interesting that your client is not sending its request as a response to a <FORM>. I read this maybe could take a peek at it and see if that helps?

    Plankton: 1% Evil, 99% Hot Gas.