in reply to Windows NT CGI File Upload Problem

I can't say for sure what your problem is, but the following code has a problem:
while ($bytesread = read($raw_file,$buffer,1024)) { print O $buffer; }
$bytesread doesn't do anything and should be skipped. My thought is that if the read doesn't get anything, having that "nothing" assigned to bytesread will cause the conditional to evaluate as true and while will write to the file regardless of the success of read. Try the following:
while (read($raw_file,$buffer,1024)) { print O $buffer; }
Please note that if you had used -w, you would have been warned that $bytesread was only used once and this might have been a significant clue.

Also, I couldn't tell at first if your filehandle was the letter 'O' or a zero. Obviously, it wasn't a zero, but I first thought that maybe it was a type causing an problem. Longer filehandles like OUTPUT are less type prone and much easier to maintain.

Cheers,
Ovid

Update: Thanks to tye for clariying that. I just knew I should have written some test code.

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

Replies are listed 'Best First'.
RE: Re: Windows NT CGI File Upload Problem
by tye (Sage) on Sep 14, 2000 at 19:50 UTC

    $bytesread doesn't do anything and should be skipped. My thought is that if the read doesn't get anything, having that "nothing" assigned to bytesread will cause the conditional to evaluate as true and while will write to the file regardless of the success of read.

    Yes and no. Yes, $bytesread is useless. No, it doesn't change the meaning of the conditional.

            - tye (but my friends call me "Tye")
RE: Re: Windows NT CGI File Upload Problem
by princepawn (Parson) on Sep 14, 2000 at 20:09 UTC
    Actually in a sense $bytesread should be doing something though... I should be ensuring that it is undef on each iteration of the loop... I think I now see the light. I should rewrite this as:
    { # read() returns the number of bytes read or undef on error $bytesread = read($raw_file,$buffer,1024); # let's exit with an error if read() returned undef die "error with file read: $!" if !defined($bytesread); # let's exit with an error if print() did not return true unless (print O $buffer) die "error with print: $!"; # let's redo the loop if we read > 0 chars on the last read redo unless !$bytesread }
    And many thanks for one of the very first posts ever from the all-great merlyn which showed me the merits of a naked do block.