Perl 的 CGI.pm 可以处理 Firefox 的<输入类型=“文件”,多个=“”>吗?表单字段?

发布于 2024-09-14 01:19:27 字数 1693 浏览 5 评论 0 原文

Firefox 3.6 引入了 [常规 type="file" 输入元素上的多个属性]( 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 上的 Data:Dumper ,它实际上什么也打印不出来。

Firefox 3.6 introduced a [multiple attribute on regular type="file" input elements](
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/).

I cannot get Perl to process these fields. I can call the field in a list context like this:

 my @files = $CGIobject->param("File_Input");

Looping through that will give me the file names as strings but nothing else.

Any suggestions would be greatly welcome.

Here's the 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>

Here is the 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;

  }

}

The script does not enter the

if (@lightweight_fh) {

block.

I've tried Data:Dumper on @lightweight_fh before the if block and it literally prints absolutely nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈甜 2024-09-21 01:19:27

使用 upload 方法.org/CGI" rel="nofollow noreferrer">CGI.pm。

在列表上下文中,upload() 将返回文件句柄数组。这使得处理多个上传字段使用相同名称的表单成为可能。

Use the upload method in CGI.pm.

In a list context, upload() will return an array of filehandles. This makes it possible to process forms that use the same name for multiple upload fields.

戈亓 2024-09-21 01:19:27

哇哦,成功了。手刹大问题?旧的 CGI.pm 版本!遗憾的是,CGI.pm 文档不包含诸如“在版本 X 中引入”等功能的注释。许多其他模块/库/包都可以。

碰巧我的版本是 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

      }

    }

  }

感谢大家的帮助。

Woohoo, got this working. The big handbrake issue? Old CGI.pm version! It's a shame the CGI.pm documentation does not include notes alongside features such as "Introduced in version X". Many other modules/libraries/packages do.

As it happens I had version 3.15 and the current is 3.49. I even got it working in strict mode. Anybody know why Stein uses non-strict examples?

Here's the 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>

Here's the 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

      }

    }

  }

Thanks for your help everyone.

早乙女 2024-09-21 01:19:27

看起来您没有遵循 处理的文档带有 CGI.pm 的文件上传字段。在我们深入探讨这一点之前,您可以使用记录的方法通过一个文件来完成此操作吗?

It doesn't look like you're following the documentation for Processing a file upload field with CGI.pm. Before we get too far into this, can you do it with one file using the documented method?

悲念泪 2024-09-21 01:19:27

是的,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/' 作为文件打开,并且不检查错误

Yes, perl's CGI.pm can proess firefox's multiple file uploads

Want to see? Use this shortcut:

use Data::Dumper;
print '<pre>', $CGIo->escapeHTML( Dumper( $CGIo ) ),'</pre>';

You'll see something like:

$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' );

First you call your file upload field File_Input, then you call it multiple_files, then you call it myfiles -- you have to use the same name, this important.

Also, $lightweight_fh and @lightweight_fh are two distinct variables, you'll need

for my $lightweight_fh ( $CGIo->upload('multiple_files') ){
    my $io_handle = $lightweight_fh->handle;
    ...
}

Also, you try to open a DIRECTORY '/hidden_deliberately/' as a file, and you don't check for errors

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文