-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cgi
executable file
·80 lines (58 loc) · 1.83 KB
/
index.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/perl
# Upload files to server
# See also:
# https://www.w3schools.com/php/php_file_upload.asp
# https://www.geeksforgeeks.org/how-to-select-multiple-files-using-html-input-tag/
use utf8;
use 5.018;
use strict;
use autodie;
use CGI qw(:standard -utf8);
#use CGI::Carp qw(fatalsToBrowser);
binmode(STDOUT, ':utf8');
print header(-charset => 'UTF-8'),
start_html(
-lang => 'en',
-title => 'Upload files',
-base => 'true',
-meta => {
'keywords' => 'upload files to server',
'viewport' => 'width=device-width, initial-scale=1.0',
},
-style => [{-src => 'css/main.css'}],
-script => [],
);
print h1("Upload files");
print start_form(
-method => 'POST',
-enctype => 'multipart/form-data',
-action => 'index.cgi',
),
q{<input type="file" name="uploaded_file" multiple>},
submit(-name => "Submit!"), end_form;
my $q = CGI->new;
if (param) {
foreach my $filehandle ($q->multi_param('uploaded_file')) {
if (!$filehandle && $q->cgi_error) {
die $q->cgi_error;
}
if ($filehandle) {
my $info = $q->uploadInfo($filehandle);
my $cd = $info->{'Content-Disposition'};
my $filename = 'unknown';
if ($cd =~ /\bfilename="(.*?)"/s) {
$filename = $1;
}
open(my $out_fh, '>', "files/$filename")
or die "Can't create file: $!";
while (read($filehandle, (my $buffer), 1024 * 1024)) {
print {$out_fh} $buffer;
}
close $out_fh;
}
else {
die "Invalid upload...";
}
}
}
print end_html;