Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / perl5 / eg / cgi / file_upload.cgi
1 #!/usr/local/bin/perl -w
2
3 use lib '..';
4 use CGI qw(:standard);
5 use CGI::Carp qw/fatalsToBrowser/;
6
7 print header();
8 print start_html("File Upload Example");
9 print strong("Version "),$CGI::VERSION,p;
10
11 print h1("File Upload Example"),
12     'This example demonstrates how to prompt the remote user to
13     select a remote file for uploading. ',
14     strong("This feature only works with Netscape 2.0 browsers."),
15     p,
16     'Select the ',cite('browser'),' button to choose a text file
17     to upload.  When you press the submit button, this script
18     will count the number of lines, words, and characters in
19     the file.';
20
21 @types = ('count lines','count words','count characters');
22
23 # Start a multipart form.
24 print start_multipart_form(),
25     "Enter the file to process:",
26     filefield('filename','',45),
27     br,
28     checkbox_group('count',\@types,\@types),
29     p,
30     reset,submit('submit','Process File'),
31     endform;
32
33 # Process the form if there is a file name entered
34 if ($file = param('filename')) {
35     $tmpfile=tmpFileName($file);
36     $mimetype = uploadInfo($file)->{'Content-Type'} || '';
37     print hr(),
38           h2($file),
39           h3($tmpfile),
40           h4("MIME Type:",em($mimetype));
41
42     my($lines,$words,$characters,@words) = (0,0,0,0);
43     while (<$file>) {
44         $lines++;
45         $words += @words=split(/\s+/);
46         $characters += length($_);
47     }
48     close $file;
49     grep($stats{$_}++,param('count'));
50     if (%stats) {
51         print strong("Lines: "),$lines,br if $stats{'count lines'};
52         print strong("Words: "),$words,br if $stats{'count words'};
53         print strong("Characters: "),$characters,br if $stats{'count characters'};
54     } else {
55         print strong("No statistics selected.");
56     }
57 }
58
59 # print cite("URL parameters: "),url_param();
60
61 print hr(),
62     a({href=>"../cgi_docs.html"},"CGI documentation"),
63     hr,
64     address(
65             a({href=>'/~lstein'},"Lincoln D. Stein")),
66     br,
67     'Last modified July 17, 1996',
68     end_html;
69