Search code examples
perlfile-uploadcgi-application

How do I upload file using CGI APPLICATION


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;
}


Solution

  • 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)) {
          ...
         }