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.

In reply to upload on different browsers by dannoura

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.