in reply to saving a file uploaded with cgi...this is driving me insane.

I might be wrong but... I don't see a place where $buffer is getting modified other than at its declaration at my $buffer = "";. If thats true (and I'm not just being a newb), the  print OUTFILE $buffer; statement is printing "" to the output file each time through the loop. Thats why the file is getting created, but has a size of 0 bytes each time (as the size of "" tends to be 0 :) ).

Replies are listed 'Best First'.
Re: Re: saving a file uploaded with cgi...this is driving me insane.
by thpfft (Chaplain) on Aug 17, 2001 at 02:09 UTC

    This part:

    while ($bytesread=read($file,$buffer,1024))

    Has the effect of repeatedly bringing 1Kbyte of data into $buffer from $file. The value returned by the read() is the number of bytes read, so once the filehandle is exhausted the while loop stops. In effect it just sucks the uploaded data through $buffer 1K at a time until it has traversed the whole filehandle.

    I agree that it's a slightly odd way round, compared to eg splice: i expect that's in order to allow exactly this construction.

    For more, see read or man CGI (from which this code mostly comes).