You use binmode() on the output file but you forgot to use binmode() on the input file. Or at least it seems to me. Either your code is incomplete or you aren't even opening $file for reading. I hope the code is incomplete, because it couldn't have worked under UNIX in this form either, as read() takes a filehandle as the first parameter and not a filename. Also, it's not necessary to use binmode() on an opened filehandle more than once before closing it.

Update : In the context of a CGI script, the filename of the uploaded file might or might not contain the full (local) path to the uploaded file. In fact, it might contain the full path to the remote file, like c:\program files\test.txt, which was where the file resided on the user machine. Also, I fixed some type globs (note the asterisk before the file handles)

Here is some code that should work on any platform, and which should also be reasonably secure against users supplying malicious filenames to trick your http server into doing stuff (I hope) :

#!/usr/bin/perl -w use strict; use HTML::Entities; local *INFILE; local *OUTFILE; my $uploadedfilename = ""; # fill this in my $fullfilename = "D:/inetpub/general/nt/cv"; # This is the name we echo back to the user : my $htmlfilename = encode_entities($uploadedfilename); die "Upload temp file $htmlfilename not found !\n" unless -x $uploaded +filename; open INFILE, "< $uploadedfilename" or die "Opening the temp file $html +filename : $!\n"; binmode INFILE; open OUTFILE, "> $fullfilename" or die "Can't create $fullfilename : $ +!\n"; binmode OUTFILE; my ($bytesread, $buffer); while ($bytesread=read(INFILE,$buffer,1024)) { print OUTFILE $buffer; } close OUTFILE; close INFILE;

Update: If you are dealing with CGI.pm, the documentation mentions the following idiom, adapted to our needs, to handle uploaded files :

$fh = $query->upload('FILE1'); binmode $fh; while ($bytesread=read($fh, $buffer, 1024)) { print OUTFILE $buffer; }
This is the correct way and should be used. Using a string as a parameter to read() is allowed but deprecated.


In reply to Re: Uploading and NT by Corion
in thread Uploading and NT by toadi

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.