*put_data = \&_write_file;

sub _write_file {
my $fmgr = shift;
my($from, $to, $type) = @_;
local *FH;
my($umask, $perms);
my $cfg = MT->config;
if ($type && $type eq 'upload') {
$umask = $cfg->UploadUmask;
$perms = $cfg->UploadPerms;
} else {
$umask = $cfg->HTMLUmask;
$perms = $cfg->HTMLPerms;
}
my $old = umask(oct $umask);
sysopen FH, $to, O_RDWR|O_CREAT|O_TRUNC, oct $perms
or return $fmgr->error(MT->translate(
"Opening local file '[_1]' failed: [_2]", $to, "$!"));
if ($type && $type eq 'upload') {
binmode(FH);
binmode($from) if $fmgr->is_handle($from);
}
## Lock file unless NoLocking specified.
flock FH, LOCK_EX unless $cfg->NoLocking;
seek FH, 0, 0;
truncate FH, 0;
my $bytes = 0;
if ($fmgr->is_handle($from)) {
while (my $len = read $from, my($block), 8192) {
print FH $block;
$bytes += $len;
}
} else {
print FH $from;
$bytes = length($from);
}
close FH;
umask($old);
$bytes;
}