send files to a ftp server in PERL

use Net::FTP;


sub sendloc2Ftp($$$){

my($FtpFullPathFile, $LocalPath, $Localfile) = @_;
my $user;
my $password;
my $host;
my $port;
my $remotePath;
#match patterns like: ftp://anonymous:anonymous@192.168.202.2:21/data
if($FtpFullPathFile =~ /ftp:\/\/(?:(\w+):(\w+)@)?((?:(?:\w+\.)+)\w+)(?::(\d+))?([\w\/]+)?/){
$user = $1;
if(not defined($1)){
$user = "anonymous";
}
$password = $2;
if(not defined($2)){
$password = "anonymous";
}
$host = $3;
$port = $4;
if(not defined($4)){
$port = 21;
}
$remotePath = $5;
if(not defined($5)){
$remotePath = "/";
}
}
else{
return $FALSE;
}
#print "host: $host, port: $port, user: $user, password: $password, remotepath: $remotePath, localpath: $LocalPath, localfile: $Localfile\n";
my $ret = send2Ftp($host, $port, $user, $password, $remotePath, $LocalPath, $Localfile);
return $ret;

}



sub send2Ftp($$$$$$$){
my ($Host, $Port, $User, $Password, $RemotePath, $LocalPath, $LocalFileName) = @_;

my $ftp = Net::FTP->new ($Host, Debug => 0) or die "sendFileViaFtp: Can't connect to the host!";
$ftp->login($User, $Password) or die "sendFileViaFtp: Can't connect to ftp with the user[$User]!";
    
    my $LocalFullPathFile;
    if(substr($LocalPath, length($LocalPath) - 1, 1) eq '/'){
    $LocalFullPathFile = $LocalPath . $LocalFileName;
    }
else{
$LocalFullPathFile = $LocalPath . '/' . $LocalFileName;
}

my $RemoteFullPathFile;
    if(substr($RemotePath, length($RemotePath) - 1, 1) eq '/'){
    $RemoteFullPathFile = "ftp://" . "$Host" . $RemotePath . $LocalFileName;
    }
    else{
    $RemoteFullPathFile = "ftp://" . "$Host" . $RemotePath . '/' . $LocalFileName;    
    }
    
    unless($ftp->cwd($RemotePath)){
    my $pwd = $ftp->pwd();
    #print "$pwd\n";
    my $errorMsg = $ftp->message();
    return $FALSE;
    }
    
    #print "$LocalFullPathFile\n$RemoteFullPathFile\n";
    unless($ftp->put($LocalFullPathFile)){
    return $FALSE;
    }
    
   $ftp->quit;
   return $TRUE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值