I have never used Net::FTP but here is a shot in the dark. When you
$ftp->get($filename), the file is being copied to the current directory of the machine running the script. Then when you
$ftp->put($filename), the file gets copied to it's final destination. Taking a quick look at the docs for Net::FTP, I did not see a method to change the local directory. In a standard ftp client this would be
lcd directoryname but this would still copy the file locally first. The one way around this would be to have either one ofthe servers run the script. That way you can get or put the files directly where you want them instead of having a third server involved to to do the transfer. In the scenario you posted, I think you will have to kill the file in your local directory after you put it using Net::FTP.
As for your other question, I think that's an easy one. Just loop through your @lines array and get all the files. Then loop through it again and put all the files. If you can get rid ofthe third server in the scenario, you will only have to do the loop once. To go through the loop only once in your current scenario, you would have to have two instances of Net::FTP and use one to get and the other to put.
Update
Here's an example (untested though)
#!/usr/bin/perl -w
use strict;
use Net::FTP;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
my $home="mysite.com";
my $username="username";
my $password="password";
my $directory="data/edit";
my $filename="data.txt";
my $away="myothersite.com";
my $awayname="awayname";
my $awaypass="awaypass";
my $awaydir="data/edit";
print "Content-type: text/html\n\n";
my $ftp1 = Net::FTP->new("$home") or die "Can't connect: 1 $@\n";
$ftp1->login($username, $password) or die "Couldn't login - 1\n";
$ftp1->cwd($directory) or die "Couldn't change directory - 1\n";
my $ftp2 = Net::FTP->new("$away") or die "Can't connect: 2 $@\n";
$ftp2->login($awayname, $awaypass) or die "Couldn't login - 2\n";
$ftp2->cwd($awaydir) or die "Couldn't change away dir
+ectory - 2\n";
my @lines = $ftp1->ls("/data/edit");
foreach (@lines)
{
$ftp1->get($filename)or die "Couldn't get $_\n";
$ftp2->put($filename) or die "Couldn't put $_\n";
print "Your file, <br>$_, has been successfully uploaded.\n";
}
$ftp1->quit;
$ftp2->quit;
unlink @lines; # This should delete all the local copies of the files
+in @lines
exit;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.