如何上传并打开使用CGI perl脚本的XAMPP Apache服务器的文件? [英] How to upload and open a file in xampp apache server using CGI Perl Script?

查看:339
本文介绍了如何上传并打开使用CGI perl脚本的XAMPP Apache服务器的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code正常工作在我的本地XAMPP Apache服务器。我运行局域网相同code具有不同的IP地址系统。该文件无法打开,我不能把它写入预期的目录。请做要紧?先谢谢了。

是通过XML文件通过以下code。

 #C:\\ XAMPP \\ perl的\\ BIN \\ perl.exe所在!
#!172.18.5.23:\\xampp\\perl\\bin\\perl.exe
#!的\\ usr \\ BIN \\ perl的-wt
#!perl的
使用严格的;
使用警告;
使用CGI;
我的$查询=新的CGI;
打印$查询 - >标题(text / html的);
打印<< END_HERE;
 < HTML和GT;
      < HEAD>
        <标题>我首先CGI脚本< /标题>
      < /头>
      <车身的bgcolor =#FFFFCC>
        < H1>欢迎的Perl CGI< / H1>
        <形式的行动=/ cgi-bin目录/ inputxml.cgi的方法=后
        ENCTYPE =的multipart / form-data的>
        < P>要上传的文件:其中;输入类型=文件名称=XML/>< / P>
        < P><输入类型=提交名称=提交值=提交表单/>< / P>
        < /表及GT;
      < /身体GT;
    < / HTML>
    END_HERE

发送xml文件到低于code .....

 #C:\\ XAMPP \\ perl的\\ BIN \\ perl.exe所在!
    #!172.18.5.23:\\xampp\\perl\\bin\\perl.exe
    #!的\\ usr \\ BIN \\ perl的-wt
    #!perl的
    使用严格的;
    使用CGI;
    使用CWD'绝对路径';
    使用CGI ::鲤鱼QW(warningsToBrowser fatalsToBrowser);
    使用文件::基名;
    $ CGI :: POST_MAX;
    我的$ safe_filename_characters =A-ZA-Z0-9 _.-
    我的$查询=新的CGI;
        我的$的cgi =新的CGI;
        我的$文件= $ CGI-GT&;参数('XML');
        我的$线;
        开(数据,< $文件)或死无法打开数据;
        打印$查询 - >头();
        $线=<数据取代;
        关闭(DATA);
        $线= 382 4 {宠儿} {} CGI灌胃;
        打印$行;
        打印绝对路径($文件);
        开(OUT,'>目录名($文件)\\\\出来_基名($文件)。');
        打印出$线;
        关闭(OUT);
        打印$查询 - >头();
    打印<< END_HERE;


解决方案

从输入参数(在这种情况下,XML),获取文件名始终是一个非常糟糕的主意。浏览器在他们的行为有所不同。有人给你完整的文件名,有的只给你的基本部分。当他们给你完整的路径,这将是在客户端计算机上的路径 - 这是pretty多少保证不会在服务器上存在的路径。 [更新:并重新阅读你的问题,我知道这也正是为什么当你在本地测试它的工作原理]

有是在如何做到这一点详细解释文档的 CGI模块。你应该写你的code前认真阅读所有的那款。但在总结。

 #获取文件名,这很可能包括路径和
#不应该被用来作为服务器上的文件名。
我的$文件名= $ CGI-GT&;参数('XML');#试算一个本地文件名
我的$ local_fn;
如果($文件名=〜/([-\\w\\.]+)$/){
  $ local_fn = $ 1;
}#获取文件句柄上传的文件
我的$ local_in_fh = $ CGI-GT&;上传('XML');#打开一个文件句柄存储文件
打开$ local_out_fh,'>',/路径/到/输出/ DIR / $ local_fn或死亡$ !;而(小于$ local_in_fh&GT){
  从输入文件#过程一行(这是在$ _)
  打印$ local_out_fh;
}

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

$ CGI-GT&; uploadInfo($文件名) - GT; {'Content-Type的'} 会给你的MIME类型上传文件。你可以用它来工作,你想如何处理该文件。

$ CGI-GT&; tmpFileName($文件名)会给你的路径到您的资料已上载的临时文件。 CGI程序退出时,这将被删除。但是,如果你只是想保存文件不以任何方式处理它,你可以将此文件移动到新位置,您的服务器上。

你的现有解决方案的一些其他注意事项:


  1. 您的第一个程序只显示一个HTML文件。那么,为什么没有一个静态的HTML文件?

  2. $ CGI :: POST_MAX 什么都不做。

  3. 请使用 CGI-方式>新而不是新的CGI

  4. 您创建两个CGI对象( $查询 $ CGI )。你只需要一个。

  5. 您的打印头CGI两次。

  6. 数据是一个特殊的文件句柄名。你不应该使用它自己的文件句柄。

  7. $线=<数据方式> 只会从文件的第一行

  8. 打开(OUT,'>目录名($文件)\\\\出来_基名($文件)')没有做像什么你什么认为它在做什么。

This code works fine in my local xampp apache server. I run the same code in local area network with different ip addressed system. The file cannot open and i cant write it to the expected directory. Kindly do the needful? Thanks in advance.

Am passing the xml file through below code.

#!"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

sending the xml file to the below code.....

#!"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;

解决方案

Getting the filename from the input parameter ("xml" in this case) is always a really bad idea. Browsers differ in their behaviour. Some give you the full filename, some only give you the basename. When they give you the full path, it will be the path on the client computer - a path that is pretty much guaranteed not to exist on your server. [Update: And re-reading your question, I realise that's exactly why it works when you're testing it locally.]

There's a detailed explanation of how to do this in the documentation for the CGI module. You should really read all of that section before writing your code. But in summary.

# 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;
}

There are a couple of other things that might be useful for you.

$cgi->uploadInfo($filename)->{'Content-Type'} will give you the MIME type of the uploaded file. You can use that to work out how you want to process the file.

$cgi->tmpFileName($filename) will give you the path to the temp file where your data has been uploaded. This will be deleted when the CGI program exits. But if you just want to save the file without processing it in any way, you can just move this file to a new location on your server.

Some other notes about your existing solution:

  1. Your first program just displays a HTML file. So why not have a static HTML file?
  2. $CGI::POST_MAX does nothing.
  3. Please use CGI->new instead of new CGI.
  4. You create two CGI objects ($query and $cgi). You only need one.
  5. You print the CGI header twice.
  6. DATA is a special file handle name. You shouldn't use it for your own file handles.
  7. $lines = <DATA> will only get the first line from the file.
  8. open(OUT, '>dirname($file)."\\out_".basename($file)') isn't doing anything like what you think it's doing.

这篇关于如何上传并打开使用CGI perl脚本的XAMPP Apache服务器的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆