File Uploading

Easiest way is with a <INPUT TYPE="FILE"> field.

Script II.4.1: upload.pl

 #!/usr/local/bin/perl
 #script: upload.pl
 
 use CGI qw/:standard/;
 
 print header,
     start_html('file upload'),
     h1('file upload');
 print_form()    unless param;
 print_results() if param;
 print end_html;
 
 sub print_form {
     print start_multipart_form(),
        filefield(-name=>'upload',-size=>60),br,
        submit(-label=>'Upload File'),
        end_form;
 }
 
 sub print_results {
     my $length;
     my $file = param('upload');
     if (!$file) {
 	print "No file uploaded.";
 	return;
     }
     print h2('File name'),$file;
     print h2('File MIME type'),
     uploadInfo($file)->{'Content-Type'};
     while (<$file>) {
 	$length += length($_);
     }
     print h2('File length'),$length;
 }

What it Looks Like

http://localhost/cgi-bin/conference/file_upload.pl
<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Sat Aug 21 13:35:20 PDT 1999