dannoura has asked for the wisdom of the Perl Monks concerning the following question:
I have a script I want to use to upload files to a website. I have 2 problems with it:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: upload on different browsers
by fizbin (Chaplain) on Jul 16, 2004 at 17:48 UTC | |
by dannoura (Pilgrim) on Jul 16, 2004 at 18:55 UTC | |
by fizbin (Chaplain) on Jul 16, 2004 at 19:18 UTC | |
by dannoura (Pilgrim) on Jul 16, 2004 at 19:26 UTC | |
by Smylers (Pilgrim) on Jul 17, 2004 at 08:50 UTC | |
by Anonymous Monk on Jul 19, 2004 at 02:04 UTC | |
|
Re: upload on different browsers
by fizbin (Chaplain) on Jul 16, 2004 at 17:58 UTC | |
|
Re: upload on different browsers
by fizbin (Chaplain) on Jul 16, 2004 at 18:05 UTC | |
|
Re: upload on different browsers
by Anonymous Monk on Jul 17, 2004 at 18:38 UTC | |
by Anonymous Monk on Jul 18, 2004 at 08:31 UTC |