summaryrefslogtreecommitdiff
path: root/eg/cgi/file_upload.cgi
blob: 1f9eaec3321d4196a3ead4e3088ad5d9a1f6107c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/local/bin/perl

use CGI qw(:standard);
use CGI::Carp;

print header();
print start_html("File Upload Example");
print strong("Version "),$CGI::VERSION,p;

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($_);
    }
    close $file;
    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.");
    }
}

print hr(),
    a({href=>"../cgi_docs.html"},"CGI documentation"),
    hr,
    address(
	    a({href=>'/~lstein'},"Lincoln D. Stein")),
    br,
    'Last modified July 17, 1996',
    end_html;