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

Hi,
I'm having some problems trying to upload some files using Perl's CGI Module. Here's the code:
use CGI; our $queryObj = new CGI; ... ... $uploadFile = $queryObj->upload("fileTextFld"); seek ($uploadFile, 0, 0); if (open (UPLOADFILE, ">$defaultdir/uploads/newFile")) { binmode UPLOADFILE; while (<$uploadFile>) { print UPLOADFILE; } } close UPLOADFILE;
Rob

Replies are listed 'Best First'.
Re: Upload files using CGI Module
by olus (Curate) on Jan 09, 2008 at 16:05 UTC
    The following example works for me.
    use CGI; my $q = CGI->new; my $fileHandle = $q->upload('file'); my $file = $q->param('file'); my $bytesread; my $buffer; open FILE , ">YOUR_NEW_FILE"; while($bytesread = read($fileHandle, $buffer, 1024)) { print FILE $buffer; } close FILE
      Thanks it worked. I'm not sure what line it was that was causing me the problem but I copied this and made the slight adjustments I needed and it worked. Does the while (<$filehandle>) { ... } work just as well or should I be using this form all the time? Thanks. Rob
        <$filehandle> also works. But in binary mode, reading large chunks of data may be more efficient.
        You are not interested in parsing the file line by line, and newline chars may be between very small groups of chars, so that would force you to do more disk accesses.