I have a script I want to use to upload files to a website. I have 2 problems with it:
- It works fine for IE, but not for Mozilla Firefox. I get no output to the browser, and the file isn't uploaded.
- Another problem is that upon upload I get a file, identical to the upload file, and named "CGItempn" where "n" is a 4 digit number. This happens regardless of whether or not the upload worked and in either browser. This means that when I use IE, I get two files, only one of which is named correctly, and when I use Firefox I get just the temp file.
The HTML is:
<FORM ENCTYPE="multipart/form-data" ACTION="../cgi-bin/upload.pl" METH
+OD="POST">
Please select a file to upload: <BR>
<INPUT TYPE="FILE" NAME="file">
<p>
<INPUT TYPE="submit">
</FORM>
And the code is:
#! c:/Perl/bin/Perl.exe -wT
# Script to upload file. This script is tied to upload.html
use strict;
use CGI;
my $query = CGI->new();
my $fn = $query->param ('file');
my $fh = $query->upload ('file');
($fn)=($fn=~/^.+\\(.+)/);
my $size=-s $fh;
my $max_size=100;
print $query->header( "text/html" ),
$query->start_html(-title => "Upload Test");
if ($size<$max_size*1024) { #This file size allowed
if (open FH, ">$fn") { # Open file
while (<$fh>) {
print FH;
}
if (check_filesize((-s FH), $size)) { # If all file copied to
+server
print $query->p("File uploaded succesfully");
} else {
print $query->p("There was a problem uploading your
file. Please ").a({-href=>"localhost/upload.html"}, "try ag
+ain");
}
} else {
print $query->p("There was a problem uploading your
file. Please ").a({-href=>"localhost/upload.html"}, "try ag
+ain");
}
} else {
print $query->p("Sorry, this file is too large. Maximum size allow
+ed is ", $max_size, "kb");
}
print $query->end_html;
sub check_filesize { # Check if upload file size is approximately equa
+l to the file written
my ($up_file, $down_file)=@_;
return 1 if ((sprintf "%d", $up_file/1024)==(sprintf "%d", $down_f
+ile/1024) ||
(sprintf "%d", $up_file/1024)==(sprintf "%d", $down_file/1024)+
+1 ||
(sprintf "%d", $up_file/1024)==(sprintf "%d", $down_file/1024)-
+1);
return 0
}
I'm using perl 5.8.0 with Apache 2.0.45 on WinXP.
Update: It seems that fizbin is right. The upload gets interrupted at some point. I can't find anything to indicate why in the apache error log. When I pass text files it's no problem (although I still have the CGItemp files), but if I upload pictures they come up corrupted. Why is this happening? Do I have to use binmode?
-----------------------------------
Any comments about coding style are welcome.