#!/usr/local/bin/perl -w use strict; use CGI; use IO::File; use POSIX qw (tmpnam); my $finalname = '/tmp/uploaded.file'; { my $tmpnam; my $html = new CGI; print $html->header; if (!defined ($html->param ('uploadname')) || $html->param ('uploadname') eq '') { print while (); print $html->end_html; exit; } my $fn = $html->param ('uploadname'); my $fh = $html->upload ('uploadname'); my $fd = ''; if (!defined ($fh)) { print "\$fh is not defined!\n"; print $html->end_html; exit; } while ((length ($fd) <= (64 * 1024)) && read ($fh, my $buffer, 1024)) { $fd .= $buffer; } if (length ($fd) == (64 * 1024)) { print "File too big!"; print $html->end_html; exit; } # # Get a temporary file name we know is ours. The loop resolves the race condition possibility. # for (;;) { $tmpnam = tmpnam (); sysopen (TMP, $tmpnam, O_RDWR | O_CREAT | O_EXCL) && last; } close (TMP); # # Create the temporary input file for the size-getter and thumbnail maker # if (open (SOURCE, ">$tmpnam")) { print SOURCE $fd; close SOURCE; } else { print "Couldn't open $tmpnam for writing: $!"; print $html->end_html; exit; } rename ($tmpnam, $finalname); print "File uploaded OK, saved as $finalname"; exit; } __DATA__ File Upload Test

Select file to upload: