in reply to Uploading Files with CGI.pm results in 0 byte files

I had this problem, there is a node somewhere on perl monks by me, I think you forgetting to set up a buffer so I'll just going to dump the code Im currently using:


use to call
$final = _upload($localfile,$destination,$filename); if ($final > 0) { #then the file was uploaded successfully! } else { return 'There was an error with the file you supplied, either it + was of 0 bytes size or the system failed!<br /> <a href="javascript: +history.go(-1);" >Back</a>'.$final; }

Destiantion is obviosuly
my $destination = "path"; my $localfile = $q->param('upload_image'); my $filename = $q->param('upload_file');

In your case swap upload_image and upload_file with uploadfile_0, i have to as my form gives the option to rename the file (just another input field). This code snippet (above) should go above the 'calling' bit
sub _upload { my $localfile = shift; my $totalbytes; my ($bytesread, $buffer); my $num_bytes = 1024; my $destination = shift; my $filename = shift; my $final; my $output_file = $destination . $filename; use CGI::Upload; open (OUTFILE, ">", "$output_file") or die "Couldn't open $output_fi +le for writing"; while ($bytesread = read($localfile, $buffer, $num_bytes)) { $totalbytes += $bytesread; print OUTFILE $buffer; } die "$output_file Read Failure: $!" unless defined($bytesread); unless (defined($totalbytes)) { # $final = "<p>Error: Could not read file $localfile, or was of zer +o length"; $final = 0; } else { # $final = "<p>Done ok, $totalbytes</p>"; $final = $totalbytes; } close OUTFILE or die "Couldn't Close file $!"; return $final; }
Barry Carlyon barry@barrycarlyon.co.uk