Perl - Copy a file (and contents) to remote server with Net:FTP and Possible Net::FTP::File
I am having a whole lot of trouble copying a file from one server to another.
I have been trying for two days.
Please know I am using strict and the below script is just for testing.
I actually get a files "name" onto the "remote" server and chmod to 0755 but it is empty.
1- The file needs to be copied/ftp'd to a "remote" servers specific directory. mkdir if !.
2- The "local" file to be copied exists in a directory other than the scripts directory.
3- After the file is copied to "remote" server, chmod it to 0755.
4- Easy for seemingly everyone but me. :-(
Below writes the file in name only, it is empty.
#!/usr/bin/perl -w
use CGI;
use CGI:开发者_如何学C:Carp qw(fatalsToBrowser warningsToBrowser);
print CGI::header();
use Net::FTP;
use Net::FTP::File;
#use strict;
my $host='ftp.myserver.com';
my $ftp=Net::FTP->new($host,Timeout=>240,Passsive=>1) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print qq~Connected<br />~;
$ftp->login('cherry@myserver.com','mypasswd') or die "Cannot login: $@";
$datedir ='04-01-2011';
#################################
$copyfrom = $datedir;
$thisfile = 'index.pl';
$fullpath = $copyfrom.'/'.$thisfile;
$path = '/home/account/public_html/folder/'.$copyfrom;
if (-e $fullpath) {print qq~Copy source exists<br >~;}
unless (-e $fullpath) {print qq~Copy source does not exist<br >~;}
#######################################################
my $isdir = $ftp->isdir($datedir);
if (!$isdir){
print qq~Remote directory does not exist<br >~;
$ftp->mkdir($datedir) or die $ftp->message;
}
if ($isdir){
print qq~Remote directory exists<br />~;
}
#############
my $new = $datedir.'/'.$thisfile;
my $isfile = $ftp->exists($new);
if (!$isfile){
print qq~Remote file does not exist<br />~;
$ftp->cwd($copyfrom);
print "I'm in the directory ", $ftp->pwd(), "\n";
my $chkdir = $ftp->isdir($copyfrom);
if ($chkdir){print qq~directory exists<br />~;}
if (!$chkdir){print qq~directory does not exists<br />~;}
$ftp->ascii() || die $ftp->message;
$ftp->put($copyfrom, $thisfile) or die $ftp->message;
$ftp->chmod(755, $thisfile) or warn $ftp->message;
}
if ($isfile){
print qq~Remote file exists<br >~;
}
$ftp->quit;
sub myerr {
print "Error: \n";
print @ERRORS;
exit 0;
}
Now, I also just tried Net::FTP::File and copy(); but, errors to browser -- Software Error "current working directory "/" ".
I have read and read tested and tested until, I find myself here.
Besides the choice of modules (I read elsewhere), what am I doing wrong?
The servers are shared servers with cpanel so, I am pretty restricted therefore, I tried these ftp modules.
I am open to a fix of my script or a whole different approach that will work on my servers.
Thanks so much for any assistance..
Ok, if I put on a blindfold and throw some darts, I will eventually hit a bull.
I did not really answer my own question but, threw the dart to the right spot.
#!/usr/bin/perl -w
##########
use CGI;
use strict;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
print CGI::header();
use Net::FTP;
use Net::FTP::File;
######################################
my $host='ftp.mysite.com';
my $username = 'cherry@mysite.com';
my $password='MycAmelsPasSwoRD';
my $dateDir ='2011-03-25';
my $path = '/home/account/public_html/localfolder/inthisfolder';
my $directory = $path.'/'.$dateDir;
my $scriptfile= 'somescript.pl';
my $file = $path.'/'.$dateDir.'/'.$scriptfile;
#################################
my $ftp = Net::FTP->new($host,Timeout=>240,Passsive=>1) or die "Cannot connect to hostname: $@";
$ftp->login($username, $password) or die $ftp->message;
$ftp->mkdir($dateDir) or die $ftp->message;
$ftp->cwd($dateDir) or die $ftp->message;
#$ftp->ascii();
$ftp->put($file) or die $ftp->message;
$ftp->chmod(755,$scriptfile) or die $ftp->message;
$ftp->quit();
I do find useful info at stackoverflow all the time, a lifesaver!
Hopefully someone will find this code I posted useful!
Some notes:
- Use
isfile
instead ofexits
when testing if a file exists. - Why do you never use
else
s? $path
is never used ($path
and$fullpath
confusion?.- Shouldn't your program
die
if local file didn't exist? - If remote file doesn't exist, you
cwd
to$copyfrom
instead of to$datedir
. - You are testing (isdir)
$copyfrom
existence aftercwd
ing to$copyfrom
, so you are really testing for/$copyfrom/$copyfrom
. Is this really what you want to do? - Almost nobody should need to set
ascii
mode.
BTW, try disabling passive mode, and try enabling debug
my $ftp=Net::FTP->new($host,Timeout=>240, Debug=>1) or $newerr=1;
精彩评论