How do I upload file using CGI APPLICATION

1.1k Views Asked by At

Hi I am trying to upload image file using perl cgi-application. Not sure what I am doing wrong - but an empty image file (with the right name) gets saved.


my $picture = $self->query->param('picture') ;
my $buffer; my $bytesread;

open (OUTFILE, ">>$user_dir/profile_picture/$picture");
while ($bytesread = read($picture, $buffer, 1024)) {
print OUTFILE $buffer;
}

2

There are 2 best solutions below

5
On BEST ANSWER

Read the " CREATING A FILE UPLOAD FIELD" section of CGI.pm documentation. There you will see that the upload method will return a filehandle.

    my $fh = $self->query->upload('picture');

    my $buffer; my $bytesread;
    while ($bytesread = read($fh, $buffer, 1024)) {
      ...
     }
0
On

Don't forget your HTML form element also needs to specify enctype='multipart/mixed', otherwise the upload filestream doesn't get sent.