【问题标题】:How to upload and open a file in xampp apache server using CGI Perl Script?如何使用 CGI Perl 脚本在 xampp apache 服务器中上传和打开文件?
【发布时间】:2014-08-20 20:53:22
【问题描述】:

此代码在我的本地 xampp apache 服务器中运行良好。我在具有不同 IP 地址系统的局域网中运行相同的代码。该文件无法打开,我无法将其写入预期的目录。请做需要的?提前致谢。

我正在通过下面的代码传递 xml 文件。

#!"C:\xampp\perl\bin\perl.exe"
#!"172.18.5.23:\xampp\perl\bin\perl.exe"
#!\usr\bin\perl -wT
#!perl
use strict;
use warnings;
use CGI;
my $query = new CGI;
print $query->header( "text/html" );
print <<END_HERE;


 <html>
      <head>
        <title>My First CGI Script</title>
      </head>
      <body bgcolor="#FFFFCC">
        <h1>Welcome to Perl CGI</h1>
        <form action="/cgi-bin/inputxml.cgi" method="post"
        enctype="multipart/form-data">
        <p>Files to Upload: <input type="file" name="xml" /></p>
        <p><input type="submit" name="Submit" value="Submit Form" /></p>
        </form>
      </body>
    </html>
    END_HERE

将xml文件发送到下面的代码.....

#!"C:\xampp\perl\bin\perl.exe"
    #!"172.18.5.23:\xampp\perl\bin\perl.exe"
    #!\usr\bin\perl -wT
    #!perl
    use strict;
    use CGI;
    use Cwd 'abs_path';
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use File::Basename;
    $CGI::POST_MAX;
    my $safe_filename_characters = "a-zA-Z0-9_.-";
    my $query = new CGI;
        my $cgi = new CGI;
        my $file = $cgi->param('xml');
        my $lines;
        open(DATA,"<$file") or die "Can't open data";
        print $query->header ( );
        $lines = <DATA>;
        close(DATA);
        $lines =~s{darling}{CGI}ig;
        print $lines;
        print abs_path($file);
        open(OUT, '>dirname($file)."\\out_".basename($file)');
        print OUT $lines;
        close(OUT);


        print $query->header ( );
    print <<END_HERE;

【问题讨论】:

  • 你需要开始检查错误,例如open(DATA,"&lt;$file") or die "Can't open data";变成open(DATA,"&lt;$file") or die "Can't open data: $!";。另见perldoc open
  • 我也是这样做的,但显示的相同错误无法从文件中打开数据。

标签: apache perl cgi


【解决方案1】:

从输入参数(在本例中为“xml”)获取文件名总是一个非常糟糕的主意。浏览器的行为不同。有些给你完整的文件名,有些只给你基本名称。当他们为您提供完整路径时,它将是 客户端 计算机上的路径 - 几乎可以保证在您的服务器上不存在该路径。 [更新:并重新阅读您的问题,我意识到这正是您在本地测试它时有效的原因。]

CGI module 的文档中有详细的explanation of how to do this。在编写代码之前,您应该真正阅读所有该部分。但总的来说。

# Get the filename which may well include a path and which
# should never be used as a filename on the server.
my $filename = $cgi->param('xml');

# Try to calculate a local filename
my $local_fn;
if ($filename =~ /([-\w\.]+)$/) {
  $local_fn = $1;
}

# Get file handle to uploaded file
my $local_in_fh = $cgi->upload('xml');

# Open a file handle to store the file
open $local_out_fh, '>', "/path/to/output/dir/$local_fn" or die $!;

while (<$local_in_fh>) {
  # process one line from the input file (which is in $_)
  print $local_out_fh;
}

还有一些其他的东西可能对你有用。

$cgi-&gt;uploadInfo($filename)-&gt;{'Content-Type'} 将为您提供上传文件的 MIME 类型。您可以使用它来确定您希望如何处理文件。

$cgi-&gt;tmpFileName($filename) 将为您提供上传数据的临时文件的路径。这将在 CGI 程序退出时被删除。但是,如果您只想保存文件而不以任何方式对其进行处理,则可以将此文件移动到服务器上的新位置。

关于您现有解决方案的一些其他说明:

  1. 您的第一个程序只显示一个 HTML 文件。那么为什么没有静态 HTML 文件呢?
  2. $CGI::POST_MAX 什么都不做。
  3. 请使用CGI-&gt;new 而不是new CGI
  4. 您创建了两个 CGI 对象($query$cgi)。您只需要一个。
  5. 您打印了两次 CGI 标头。
  6. DATA 是一个特殊的文件句柄名称。您不应该将它用于您自己的文件句柄。
  7. $lines = &lt;DATA&gt; 只会从文件中获取第一行。
  8. open(OUT, '&gt;dirname($file)."\\out_".basename($file)') 并没有像您认为的那样做任何事情。

【讨论】:

    猜你喜欢
    • 2014-10-13
    • 2014-03-27
    • 2014-12-14
    • 1970-01-01
    • 2016-12-08
    • 2018-01-30
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多