#!/usr/local/bin/perl # # uploader.cgi -- This perl CGI script will print out either the submitted data # or the content of the uploaded file. # For NT users, make sure you call the binmode() function # after the filehandle is created when you try to save a # binary file. # # Last modified : 29th.Mar.1999 # # Simon Tneoh Chee-Boon tneohcb@pc.jaring.my # # Copyright (c) 1998,1999 Simon Tneoh Chee-Boon. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. use strict; use Carp; # # We use the CGI_LIB module and create an object to store the data. # use CGI_LIB; my($cgi) = new CGI_LIB; if ($cgi->{'ViewOption'} eq " View Uploaded File ") { # # Users want to see how the uploaded file is. # # # Here I save the uploaded file to a temporary file. # If you want to save the uploaded file with its original filename, # you can do something like the followings: # my($filename) = $cgi->{'file_type'}->{'filename'} =~ /[\/\\]([^\/\\]+)$/; # open(FILE, "> $filename") || confess "Fail to create the file, $filename: $!"; # print FILE $cgi->{'file_type'}->{'contents'}; # close(FILE); # p/s: "file_type" is the field name inside the "input" tag. # open(TMP, "> temporaryfile") || confess "Fail to create the file, temporaryfile: $!"; print TMP $cgi->{'file_type'}->{'contents'}; close(TMP); # # Print the HTTP headers and the content of the uploaded file. # p/s: 'Content-Disposition: filename="FILENAME"' line is used so that # when the users download the file using the Netscape browser, the # browser will prompt the users whether to save the file with the # filename, FILENAME, or not. # print "Content-Type: $cgi->{'file_type'}->{'content-type'}\n"; print "Content-Disposition: filename=\"$cgi->{'file_type'}->{'filename'}\"\n\n"; print $cgi->{'file_type'}->{'contents'}; } elsif ($cgi->{'ViewOption'} eq " View Variables ") { # # Users want to know how to get the submitted data. # # # Print the HTTP header and HTML tags. # print "Content-Type: text/html\n\n"; print "\n"; print "
\n"; print "\n"; # # We will print out all the submitted data now. # my($key); for $key (sort keys %{$cgi}) { print "\n"; print "\n"; print "
\n"; my($ref) = ref($cgi->{$key}); if ($ref eq "HASH") { # # This is a hash array. # printTableHead("Hash Array", $key); for (keys %{$cgi->{$key}}) { printTableRow("\$cgi->{'$key'}->{'$_'}", $cgi->{$key}->{$_}); } } elsif ($ref eq "ARRAY") { # # This is an array. # printTableHead("Array", $key); my($count) = 0; for (@{$cgi->{$key}}) { printTableRow("\$cgi->{'$key'}\[$count\]", $cgi->{$key}[$count]); $count++; } } else { # # This is just a normal variable which contains only one value. # printTableHead("Single Value", $key); printTableRow("\$cgi->{'$key'}", $cgi->{$key}); } print " \n"; } print "