Skip navigation.
Your source for Perl tips, howto's, faq and tutorials
( categories: )

Use the functions provided by the CGI module. You should define the form with 'start_multipart_form()' and the upload field with 'filefield()'. The file is uploaded to a temporary location; you have to write to another location in order to keep it.

Have a look at the following example to see a working script:


use CGI qw(:standard);
#-- use CGI::Carp to print error messages to the browser
use CGI::Carp qw(fatalsToBrowser);
#-- where to save the uploaded file
my $upload_dir = "/tmp";
#-- create the form
print header,
     start_html(-title=>'Upload page'),
     'File: ', filefield('input_file'), p,
#-- if 'Submit' button is pressed
if ( param('Submit') )
   #-- 'upload' returns a file handle to the uploaded file
   my $fh = upload('input_file');
   die "Can't upload file: $!" . cgi_error() . "\n" if ( ! defined($fh) );
   #-- get the name of the uploaded file
   my $filename = param('input_file');
   #-- save the file in the desired location
   open FILE, ">$upload_dir/$filename" or die "Can't create upload file: $!\n";
   while ( <$fh> )
     print FILE;
   close FILE;
   print "file uploaded successfuly";