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

Monks,

From everything I have read, the following code should work. I'm trying to upload a file and then save it on the server. It is creating the file, but does not write the contents from the uploaded file into it. I'm getting a file with 0 bytes. Any ideas what is wrong?
#!/usr/bin/perl -w # Display all erors to screen $|++; # autoflush buffers; use CGI::Carp 'fatalsToBrowser'; print "Content-type: text/html\n\n"; use strict; use CGI qw/:standard/; my $infile = upload('file') or die "File was not uploaded correctly\n" +; test(); display_page(); open (OUT, ">/tmp/uploaded") or die "Can't open file: $!\n"; sub test { my $buffer; while ( read($infile,$buffer,1024) ) { print OUT $buffer; } } sub display_page { my $message = "$_[0]"; print header, start_html( "-title" => "Results Page"), p(" "), p("$infile was uploaded correctly" ), p(" "), end_html; }


Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
(cLive ;-) Re: More CGI File Upload Woes
by cLive ;-) (Prior) on Apr 24, 2002 at 20:39 UTC
    might help if you open OUT before you call the sub - or better still, put the open IN the sub :)

    And, to be neat, close the filehandle when done...

    cLive ;-)


      duh, thanks I'm an idiot. That was the problem.

      Thanks,
      Dru
      Another satisfied monk.
Re: More CGI File Upload Woes
by Moonie (Friar) on Apr 24, 2002 at 21:04 UTC
Re: More CGI File Upload Woes
by bassplayer (Monsignor) on Apr 24, 2002 at 20:27 UTC
    Is the enctype attribute of your <form> tag set to multipart/form-data?

    bassplayer