#!/usr/bin/perl -T use strict; use warnings; use CGI; use CGI::Carp qw/ fatalsToBrowser /; use Template; my $cgi = CGI->new; my $template_vars = { cgi_version => $CGI::VERSION, }; # Process the form if there is a file name entered if ( my $file = $cgi->param( 'filename' ) ) { die "filename passed as ARG" if $file =~ /ARG/; my $tmpfile = $cgi->tmpFileName( $file ); my $mimetype = $cgi->uploadInfo( $file )->{'Content-Type'} || ''; @{$template_vars}{qw/file temp_file mimetype/} = ( $file,$tmpfile,$mimetype ); my %wanted = map { $_ => 1 } $cgi->multi_param( 'count' ); while ( <$file> ) { $template_vars->{lines}++ if $wanted{"count lines"}; $template_vars->{words} += split(/\s+/) if $wanted{"count words"}; $template_vars->{chars} += length if $wanted{"count chars"}; } close( $file ); } print $cgi->header( -type => 'text/html', -charset => 'utf-8', ); my $tt = Template->new; $tt->process( \*DATA,$template_vars ) or warn $tt->error; __DATA__ File Upload Example Version [% cgi_version %]

File Upload Example

This example demonstrates how to prompt the remote user to select a remote file for uploading.

Select the 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.

Enter the file to process:

[% IF file.defined %]

[% file %]

[% temp_file %]

MIME Type: [% mime_type %]

Lines: [% lines %]
Words: [% words %]
Characters: [% chars %]
[% END %]