Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
上传到Web服务器Perl两端_Perl_File Upload_Cgi_Multipartform Data - Fatal编程技术网

上传到Web服务器Perl两端

上传到Web服务器Perl两端,perl,file-upload,cgi,multipartform-data,Perl,File Upload,Cgi,Multipartform Data,我有两个Linux机器。一个很远。我为我遥远的Linux机箱提供计算服务 使用LAMP(Linux Apache Mysql Perl)服务器。有一天,我想如果有一个脚本(我远方的工作人员不懂电脑)可以轻松地将文件从那里传输到这里,那该多好啊。我觉得通过CGI上传文件很容易。考虑到服务器是https:,我可以免费获得加密。已经和它斗争了几天了,什么都没用。我在网上找到的样本不起作用。所涉及包的文档太模糊了 服务器端: #!/usr/bin/perl -wT use strict; use CGI

我有两个Linux机器。一个很远。我为我遥远的Linux机箱提供计算服务 使用LAMP(Linux Apache Mysql Perl)服务器。有一天,我想如果有一个脚本(我远方的工作人员不懂电脑)可以轻松地将文件从那里传输到这里,那该多好啊。我觉得通过CGI上传文件很容易。考虑到服务器是https:,我可以免费获得加密。已经和它斗争了几天了,什么都没用。我在网上找到的样本不起作用。所涉及包的文档太模糊了

服务器端:

#!/usr/bin/perl -wT
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
use Sys::Syslog;
use Data::Dumper;

$CGI::POST_MAX = 1024 * 5000;
$CGI::DISABLE_UPLOADS = 0;


my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "/var/my_safe_dir_writable_by_user_nobody";
my $query = new CGI;

#my $filename = $query->param( 'upload' );
my $filename = $query->param( 'filename' );
openlog( 'bare_upchuck.cgi','nodelay,pid','local1' );

if ( !$filename )
    {
    print $query->header ( );
    syslog( 'debug', "No filename!" );
    exit;
    }

# remove any absolute directories
my ( $name, $path, $extension ) = fileparse ( $filename, '..*' );
$filename = $name . $extension;

# remove any spaces.
$filename =~ tr/ /_/;

# remove any unsafe characters
$filename =~ s/[^$safe_filename_characters]//g;

# untaint the filename, taking only safe characters.
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
    {
    $filename = $1;
    }
else
    {
    die "Filename contains invalid characters";
    }

syslog( 'debug', "now filename is $filename" );
syslog( 'debug', CGI::Dump() );

my $upload_filehandle = $query->upload($filename );

if( !defined $upload_filehandle )
    {
    syslog ( 'debug', "upload_filehandle is undefined!" );
    }
else
    {
    syslog ( 'debug', "upload_filehandle is $upload_filehandle" );
    }
open( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;

while( <$upload_filehandle> )
    {
    print UPLOADFILE;
    }
close UPLOADFILE;
#!/usr/bin/perl -d
use HTTP::Request::Common;
use LWP::UserAgent;
use CGI qw(:standard);
use strict;
use warnings;

my $file;
my $result;
my $message;
my $filePath = '/where_files_to_upload_are';

$file = $filePath . '/' . "stuff.pdf";  #tryit

my $ua = LWP::UserAgent->new;
my $req = $ua->request( POST 'https://www.jm-properties.com/jt5/upchuck/bare_upchuck.cgi',
      Content_Type => 'form-data',
      Content => [ Content_Type => "application/pdf",
                   filename => "stuff.pdf",
                   Upload => ["$file" ]
                 ]
);

print "\nRESPONSE -- \n" . $req->as_string;

print $req->content;
# Check the outcome of the response
if ($req->is_success)
    {
    print $req->content;
    }
else
    {
    print "\n in else not success\n";
    }
…服务器端主要使用CGI.pm,客户端使用-LWP::UserAgent。发生的情况是,文件似乎没有传输。服务器端因未定义的$upload\u文件句柄而失败。在客户机上执行扫描显示文件正在打开和读取

该文件名被服务器看到,并成功清除。但是(服务器中)似乎没有实际的文件。也许有人在那里做过这样的事,可以看到一些明显的东西?我已经把头撞在墙上好几天了,开始疼了:)

可能的问题可能包括客户端用于设置上载的Perl数据结构。另外-将错误的文件名传输到服务器,因此$query->upload()调用失败。但我真的不知道。我甚至不知道如何接近 故障排除。由于服务器端脚本是一个CGI,因此我无法在其上真正使用Perl调试器。我在客户端尝试了调试器,这似乎做了一些明智的事情。它确实到达了HTTP::Common::Request::Post,正如我所说,strace验证文件是否正在被打开和读取。 我在网络上发现的这些示例非常古老,API已经漂移


提前谢谢,

好的,我就是这么做的。我并没有真正让它工作起来,但我取得了实质性的进步,正如这里有人建议的那样,我转向了一种不同的技术

为了将问题分成几部分,我编写了一个假“Web服务器”,它位于8080端口,除了将传入的网络请求输出到STDOUT之外,什么也不做。然后我用管道将它们传输到一个文件中

每个表单尝试不同的上载脚本。其中一个是假服务器。 通过从浏览器上传到假服务器,我捕捉到了一个真实的、有效的 文件上传看起来像

然后,我在远程机器上运行了我的perl脚本,试图上传到服务器上,并捕获了我的错误上传的转储

问题很明显,我可以在客户端使用脚本来获得可编辑的输出

然而,尽管我可能会尝试,但我无法让CGI.pm在服务器上接受该输出。我连续几天把头撞在墙上。它将获得文件名,但打开内部生成的临时文件的文件句柄的魔法是返回undef

然后,我用PHP对服务器端进行了重新编码(我知道这大约是Shuck),并在15分钟内使其正常工作

我希望这能让别人少一点悲伤。CGI.pm上传工具的文档记录很差,而且已经损坏


我尝试在这里插入代码,但没有时间或精力在每行前面手动插入4个空格。

rsync
可能更简单,因为您希望传输文件并在两个框上都具有shell访问权限。实际上,
scp
rsync
有什么问题吗?它们也给你免费加密。老实说,可能什么都没有。我只是没想到这些。