Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I've been away from Perl for almost 3 months now. One of my first tasks, now that I'm back, is to create a file upload script. That's not a problem. I've done enough of these that I can code them in my sleep.

Wrong.

What am I missing with the following test case?

#!D:\perl\bin\perl.exe -T use strict; use warnings; use CGI; use HTML::Entities; use constant UPLDTMPDIR => './newDocs/'; use constant ZIPTEMPFILE => 'zip~tempfile.zip'; $CGI::DISABLE_UPLOAD = 0; # Temporarily reenable uploads $CGI::POST_MAX = 1_048_576; my $cgi = CGI->new; print $cgi->header, $cgi->start_html( -title => "Test" ); get_upload(); print $cgi->h1( "It worked. Stop whining and get this done!" ), $cgi->end_html; sub get_upload { $cgi->cgi_error and error( "Error uploading file: " . $cgi->cgi_er +ror ); my $zipFile = $cgi->upload( "zipfile" ) or error( $cgi->p( "No fil +e uploaded." ) ); my $format = $cgi->uploadInfo( $zipFile )->{ 'Content-Type' }; if ( $format ne 'application/x-zip-compressed' ) { error($cgi->p( "Illegal file format." ) ); } open FH, ">", UPLDTMPDIR.ZIPTEMPFILE or die "Can't open ".UPLDTMPDIR.ZIPTEMPFILE." for writing: $!" +; { local $/; binmode $zipFile; print FH <$zipFile>; } close FH or die "Can't close ".UPLDTMPDIR.ZIPTEMPFILE.": $!"; }

Everything works perfectly, but I am uploading a zip file that is 31,113 bytes. It's getting saved as 31,217 bytes. When I try to unzip the archive, it's identified as a corrupted. My only guess is that the line endings are being translated, but I thought binmode would take care of that. Heck, I'm embarrassed to be asking this because it's usually the sort of question that I answer :)

Configuration info:

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
(tye)Re: Yet another file uploading problem.
by tye (Sage) on Apr 20, 2001 at 00:06 UTC
    binmode FH;         - tye (but my friends call me "Tye")

      AAAAAAAAAAAAGGGGGGGGGGGGGGHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!

      Time for me to take down my CGI course, go back to newbie school and cry myself to sleep. This is going to be a loooong, day. I can tell. Thanks, tye. :)

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Yet another file uploading problem.
by TStanley (Canon) on Apr 20, 2001 at 00:18 UTC
    This comes straight from the camel's mouth (page 685, para 1):
    binmode binmode FILEHANDLE, DISCIPLINES binmode FILEHANDLE This function arranges for the FILEHANDLE to have the semantics specified by the DISCIPLINES argument. If DISCIPLINES is omitted, binary (or "raw") semantics are applied to the filehandle.

    TStanley
    In the end, there can be only one!