Firefox 3.6引入了[常规类型=“文件”输入元素的多重属性]( http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/)。
我无法让Perl处理这些字段。我可以在列表上下文中调用该字段,如下所示:
my @files = $CGIobject->param("File_Input");
循环显示将文件名作为字符串,但没有别的。
非常欢迎任何建议。
这是HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Multiple file upload test</title>
</head>
<body>
<form action="deliberately_obfuscated"
method="post"
enctype="multipart/form-data">
<input type="file"
name="multiple_files"
multiple="true"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
这是Perl:
#!/usr/bin/perl
#use strict;
#use warnings;
use CGI;
my $CGIo = new CGI;
print $CGIo->header();
@lightweight_fh = $CGIo->upload('myfiles');
# undef may be returned
# if it's not a
# valid file handle
if (@lightweight_fh) {
# Upgrade the handle to
# one compatible with IO::Handle:
my $io_handle = $lightweight_fh->handle;
open (OUTFILE,'>>','/hidden_deliberately/');
while ($bytesread = $io_handle->read($buffer,1024)){
print OUTFILE $buffer;
}
}
脚本不会进入
if (@lightweight_fh) {
块。
我在if块之前尝试了数据:@lightweight_fh上的Dumper,它确实没有打印任何内容。
答案 0 :(得分:6)
答案 1 :(得分:5)
碰巧我的版本是3.15,目前是3.49。我甚至让它以严格的模式工作。有人知道斯坦因为什么使用非严格的例子吗?
这是XHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Multiple file upload test</title>
</head>
<body>
<form action="deliberately_hidden"
method="post"
enctype="multipart/form-data">
<input type="file"
name="multiple_files"
multiple="true"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
这是Perl:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
my $CGIo = new CGI;
print $CGIo->header();
my @lightweight_fh = $CGIo->upload('multiple_files');
foreach my $fh (@lightweight_fh) {
# undef may be returned if it's not a valid file handle
if (defined $fh) {
# Upgrade the handle to one compatible with IO::Handle:
my $io_handle = $fh->handle;
open (OUTFILE,'>>','/deliberately_hidden/' . $fh);
while (my $bytesread = $io_handle->read(my $buffer,1024)) {
print OUTFILE $buffer
}
}
}
感谢大家的帮助。
答案 2 :(得分:3)
您看起来并不像CGI.pm那样关注Processing a file upload field的文档。在我们深入研究之前,您是否可以使用记录的方法使用一个文件来完成它?
答案 3 :(得分:1)
是的,perl的CGI.pm可以使firefox的多个文件上传
想看?使用此快捷方式:
use Data::Dumper;
print '<pre>', $CGIo->escapeHTML( Dumper( $CGIo ) ),'</pre>';
你会看到类似的东西:
$VAR1 = bless( {
'.parameters' => [
'filename',
'submit'
],
'use_tempfile' => 1,
'.tmpfiles' => {
'*Fh::fh00003temp-2.txt' => {
'info' => {
'Content-Type' => 'text/plain',
'Content-Disposition' => 'form-data; name="filename"; filename="temp-2.txt"'
},
'name' => bless( do{\(my $o = 'C:\\WINDOWS\\TEMP\\CGItemp52869')}, 'CGITempFile' ),
'hndl' => bless( \*{'Fh::fh00003temp-2.txt'}, 'Fh' )
},
'*Fh::fh00001temp-1.txt' => {
'info' => {
'Content-Type' => 'text/plain',
'Content-Disposition' => 'form-data; name="filename"; filename="temp-1.txt"'
},
'name' => bless( do{\(my $o = 'C:\\WINDOWS\\TEMP\\CGItemp52775')}, 'CGITempFile' ),
'hndl' => bless( \*{'Fh::fh00001temp-1.txt'}, 'Fh' )
}
},
'.charset' => 'ISO-8859-1',
'param' => {
'filename' => [
$VAR1->{'.tmpfiles'}{'*Fh::fh00001temp-1.txt'}{'hndl'},
$VAR1->{'.tmpfiles'}{'*Fh::fh00003temp-2.txt'}{'hndl'}
],
'submit' => [
'Process File'
]
},
'escape' => 1,
'.header_printed' => 1
}, 'CGI' );
首先调用文件上传字段File_Input,然后将其称为multiple_files,然后将其称为myfiles - 您必须使用相同的名称,这很重要。
另外,$ lightweight_fh和@lightweight_fh是两个不同的变量,你需要
for my $lightweight_fh ( $CGIo->upload('multiple_files') ){
my $io_handle = $lightweight_fh->handle;
...
}
此外,您尝试将DIRECTORY'/ hidden_deliberately /'作为文件打开,并且不检查错误