saving an uploaded file

I have an HTML form which has a field in which the user can specify a file to be uploaded to the HTTP server.
I am now writing the perl script which will save it locally (on the HTTP server machine).  I am looking for sample code which shows how to do this save.  Specifically, I'm wondering how to get the file handle for the uploaded file or do I even need the file handle to do the save?
Who is Participating?
makerpConnect With a Mentor Commented:
# wriiten by paul maker 2000
# example to demonstate uploading a file via a file contol on a web page
# * no validation is made on file type/size etc.....
# you will want to validate size to stop denial of service attacks etc
# use the cgi package
use CGI;
# create a cgi object
$query = new CGI;

# if we have no parameters then display the form
     print $query->header();
          <FORM METHOD=POST ENCTYPE=multipart/form-data>
               Enter your name and select a file for upload :
               <INPUT TYPE=TEXT NAME=name>
               <INPUT TYPE=FILE NAME=file>
               <INPUT TYPE=SUBMIT VALUE=upload>
     # get the name the user entered on the form
     $name = $query->param('name')
     # get the filename (this is also a filehandle as will be demonstated shortly)
     $filename = $query->param('file');

     # spilt it so we can get the filename and extension
     @parts = split /(\\)|(\/)/,$filename;
     # get the last item from the array
     $save_name = @parts[$#parts];
     # open the file (make sure the web server account has write permissions on the directory)
     open(OUTFILE,">".$save_name) or die("cannt copy image over ! ... ".$!);
     # only set binmode if you are sure a binary file is coming over
     binmode OUTFILE;
     # use the filehandle $filename read it in and write it out to our OUTFILE
     while ($bytesread = read($filename,$buffer,1024))
          print OUTFILE $buffer;
     # redirect them to our form
     print $query->redirect('');
$file = $query->param('fileComeIn');
# fileComeIn will be upload field name in the form
while( $bytesread = read($file, $dataread, 1024) )
    print TEMPHANDLE $dataread
you may want to check for size of the uploaded file by sum up bytesread and check the sum
before you write to your local TEMP file
open TEMPHANDLE "> ./temp/uploaded.file" or die "failed to write temp file: $!\n"
You can also use POST_MAX to limit upload size. use Carp 'fatalsToBrowser'
to complain to the user if file is too big.
without that, user will get a 500 server error. As long as you know what's going on.
my $sizeLimitInK = 1024;
$CGI::POST_MAX = 1024 * $sizeLimitInK;
All Courses

From novice to tech pro — start learning today.