Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Upload files using CGI Module

by rpike (Scribe)
on Jan 09, 2008 at 15:37 UTC ( [id://661379]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://661379]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found