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