CGI and Perl

Listing 5.3. Upload a file to the server

#!/usr/local/bin/perl
 use CGI qw(:standard);
 use CGI::Carp;
 print header();
 print start_html("Example 3: Upload File to Server");
 print h1("File Upload Example"),
     `This example demonstrates how to prompt the remote user to
     select a remote file for uploading. `,
     strong("This feature only works with Netscape 2.0 browsers."),
     p,
     `Select the `,cite(`browser'),' button to choose a text file
     to upload.  When you press the submit button, this script
     will count the number of lines, words, and characters in
     the file.';
 @types = (`count lines','count words','count characters');
 # Start a multipart form.
 print start_multipart_form(),
     "Enter the file to process:",
     filefield(`filename','',45),
     br,
     checkbox_group(`count',\@types,\@types),
     p,
     reset,submit(`submit','Process File'),
     endform;
 # Process the form if there is a file name entered
 if ($file = param(`filename')) {
     $tmpfile=tmpFileName($file);
     print hr(),
           h2($file),
           h3($tmpfile);
     my($lines,$words,$characters,@words) = (0,0,0,0);
     while (<$file>) {
         $lines++;
         $words += @words=split(/\s+/);
         $characters += length($_);
     }
     grep($stats{$_}++,param(`count'));
     if (%stats) {
         print strong("Lines: "),$lines,br if $stats{`count lines'};
         print strong("Words: "),$words,br if $stats{`count words'};
         print strong("Characters: "),$characters,br if $stats{`count characters'};
     } else {
         print strong("No statistics selected.");
     }
 }
 end_html;

Output from Example 3
Figure 5.5. Output from Example 3.