add CGI-2.42, its and testsuite
[p5sagit/p5-mst-13.2.git] / eg / cgi / file_upload.cgi
CommitLineData
424ec8fa 1#!/usr/local/bin/perl -w
54310121 2
424ec8fa 3use lib '..';
54310121 4use CGI qw(:standard);
424ec8fa 5use CGI::Carp qw/fatalsToBrowser/;
54310121 6
7print header();
8print start_html("File Upload Example");
9print strong("Version "),$CGI::VERSION,p;
10
11print 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.
24print 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
34if ($file = param('filename')) {
35 $tmpfile=tmpFileName($file);
424ec8fa 36 $mimetype = uploadInfo($file)->{'Content-Type'} || '';
54310121 37 print hr(),
38 h2($file),
424ec8fa 39 h3($tmpfile),
40 h4("MIME Type:",em($mimetype));
41
54310121 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
424ec8fa 59# print cite("URL parameters: "),url_param();
60
54310121 61print 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