App-ZofCMS
view release on metacpan or search on metacpan
lib/App/ZofCMS/Plugin/FileUpload.pm view on Meta::CPAN
);
redo UNIQUE_NAME if -e $upload->{name};
}
}
elsif ( ref $upload->{name} eq 'SCALAR' ) {
$upload->{name} = ( splitpath $remote_filename )[-1];
$upload->{name} =~ s/(?<=[^.])[.].+$//;
$upload->{name} = catfile(
$upload->{path},
$upload->{name} . $upload->{ext}
);
}
elsif ( ref $upload->{name} eq 'SCALAR' ) {
$upload->{name} = catfile(
$upload->{path},
( $remote_filename =~ /([^.]+)(?:[.].+)?$/)[0] . $upload->{ext}
);
}
else {
$upload->{name} = catfile(
$upload->{path},
$upload->{name} . $upload->{ext}
);
}
if ( $upload->{name_filter} ) {
$upload->{name} =~ s/$upload->{name_filter}//g;
}
my $upload_info = $cgi->uploadInfo( $remote_filename );
if ( defined $upload->{content_type} ) {
$upload->{content_type} = [ $upload->{content_type} ]
unless ref $upload->{content_type} eq 'ARRAY';
unless ( grep { $upload_info->{'Content-Type'} eq $_ }
@{ $upload->{content_type} }
) {
$template->{t}{ $error_key } = 'Invalid file type';
return;
}
}
my $fh = $cgi->upload( $upload->{query} );
if ( not $fh and $cgi->cgi_error ) {
$template->{t}{ $error_key } = $cgi->cgi_error;
return;
}
return
unless $fh;
my $fh_out;
unless ( open $fh_out, '>', $upload->{name} ) {
$template->{t}{ $error_key } = "Failed to open local file [$!]";
return;
}
seek $fh, 0, 0;
binmode $fh;
binmode $fh_out;
{
local $/ = \1024;
while ( <$fh> ) {
print $fh_out $_;
}
}
close $fh;
close $fh_out;
if ( ref $upload->{on_success} ) {
$upload->{on_success}->(
$upload->{name}, $template, $query, $config,
);
}
$template->{t}{ $success_key } = 1;
$template->{t}{ $filename_key } = $upload->{name};
return 1;
}
1;
__END__
=encoding utf8
=head1 NAME
App::ZofCMS::Plugin::FileUpload - ZofCMS plugin to handle file uploads
=head1 SYNOPSIS
In your ZofCMS template:
file_upload => {
query => 'uploaded_file',
},
plugins => [ qw/FileUpload/ ],
In your L<HTML::Template> template:
<tmpl_if name="upload_error">
<p class="error">Upload failed: <tmpl_var name="upload_error">
</tmpl_if>
<tmpl_if name="upload_success">
<p>Upload succeeded: <tmpl_var name="upload_filename"></p>
</tmpl_if>
<form action="" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="uploaded_file">
<input type="submit" value="Upload">
</div>
</form>
=head1 DESCRIPTION
The module is a ZofCMS plugin which provides means to easily handle file
uploads.
( run in 0.770 second using v1.01-cache-2.11-cpan-39bf76dae61 )