On Tue, 15 Aug 2000, Ian Mahuron wrote:
> What is the proper way to take the filehandle provided by $upload->fh and write the
>file to disk? I seem to be having buffering
> issues w/ the following (file is truncated):
>
> if (open(OUTFILE, ">$u_fullpath")) {
> print OUTFILE $buffer while read($fh, $buffer, 8*1024);
> } else {
> ...
> }
>
> a simple $| = 1; before the print didn't seem to help.. maybe OUTFILE is not hot?
>Dunno.. this is my first run in w/ buffering
> issues. I hope someone can help.
>
> TIA
>
> Ian
>
>
here's what i do in one of my modules:
my $file_size = $upload->size || 0;
if ($file_size == 0) {
return $self->err_out("You didn't supply a valid file.");
}
#
# write the data to file, making sure what gets
# written is the same as what we expected
#
my $umask = umask 02;
eval {
no strict 'refs';
my $fh = $upload->fh or die 'No file handle';
my $data = '';
my $length = 0;
{
local *F;
open(F, "> $file_path") or die "Failed to open '$file_path': $!";
while ( read($fh, $data, 4096) ) {
print F $data;
$length += length($data);
}
close F;
}
if ($length != $file_size) {
die "Failed to write '$file_path' to file";
}
};
#
# reset umask
#
umask $umask;
hth,
ky