# open a new file and transfer bit by bit from what's in the buffer open (SAVED,">>$localfile") || die $!; while ($bytesread=read($remotefile,$buffer,1024)) { print SAVED $buffer; } close SAVED; chmod $mode, '$filename';