Perl , uploading empty file.

Hi

The below script used to work fine. Suddenly it's uploading empty file. I am very new to perl. Please help me to find out the problem.

#!/usr/bin/perl
#script: upload.pl

use CGI qw/:standard/;

print header,
    start_html('File upload');
print_form();
print_results() if param;
print end_html;

sub print_form {
  print h3('Upload Fusion File');
  
  print start_multipart_form(),
    filefield(-name=>'upload',-size=>40),
    submit(-label=>'Upload File'),
    end_form;
}

sub print_results {
  my $length;
  my $file = param('upload');
  print "<hr>";
  if (!$file) {
     print '<br>Please select file to upload';
  } else {
    ($filename) = (split /\\/ , (split /\// , $file)[-1])[-1];
    #$filename = lc $filename;
    open(UPLOAD, ">./fusion_files/$filename") || 
      print "Cannot open $filename: $!";

    while (<$file>) {
      $length += length($_);
      print UPLOAD $_;
    }
    close(UPLOAD);
    print '<br><b>Showing process activities</b>';
    print '<br>File Name : ',$filename, '<br>File Size: ',$length,' bytes';
    print '<br>Starting main processing.....<br>This may take few minutes depending on number of lines in the file.';
    print '<SCRIPT language="JavaScript"> top.work.frame3.location.href="'.$ENV{'PANEL_WEB'}.'/adr/core/load/?M=D";</SCRIPT>';
  }
}

Can we see the entire response page?

From the give code, I see one issue. You should open the $file for reading before the while loop.

    open INFILE, "<$file" || die;
    while (<INFILE>) {
      $length += length($_);
      print UPLOAD $_;
    }