Most people don't seem to think of using -s on the filehandle provided by CGI::Simple (or CGI). The following script works just fine (ugly example, but it works). You will notice one problem with this however: it works for the reading, but the entire file has to be uploaded before processing can begin. As far as I know, this problem exists in every single upload and cannot be avoided. The code in the script won't even start executing until the client has passed the entire file upload contents.
#!c:/perl/bin/perl -w
$| = 1;
use strict;
use CGI::Simple qw(-upload);
my $q = CGI::Simple->new();
if ( defined($q->param('fh_upload')) ) {
my $fh = $q->upload( $q->param('fh_upload') );
my $size = -s($fh);
print $q->header(), <<'END_HTML';
<html>
<head>
<title>Upload Example</title>
</head>
<body>
END_HTML
my ($uploaded, $buffer);
while ( my $bytes = read($fh, $buffer, 1024) ) {
$uploaded += $bytes;
print int($uploaded / $size * 100), "%<br />\n";
select( undef, undef, undef, 0.02);
}
print "</body></html>";
}
else {
print $q->header();
print <<"END_HTML";
<html>
<head>
<title>Upload Example</title>
</head>
<body>
<form action="${\($ENV{'REQUEST_URI'})}" method="post" enctype
+="multipart/form-data">
File to Upload: <input type="file" name="fh_upload" size="
+30" />
<input type="submit" value="upload file" />
</form>
</body>
</html>
END_HTML
}
|