|
|
OverviewThis is not a CGI. This script is executed from a command line. This script allows to upload a CGI to multiple directories.
Usage of the commandputall.pl <filename> <username> <password> The script name is putall.pl. Execute this command in the directory the file you upload is there. First argument is the file you want to upload. The second argument is the username of the FTP site and the third one is password of the FTP site. Please do not include path name into the file name.
Flow
A sample code
use Net::FTP;
my %db = (
'site' => "ftp.yoursite.com",
'root' => "subdir/cgi-bin",
'subdir' => [qw(
dir1
dir2
dir3
dir4
dir5
)],
);
die "Usage: $0 <filename> <username> <password>\n"
if ($#ARGV < 2);
my $ftpsite = $db{site};
my $filename = $ARGV[0];
my $username = $ARGV[1];
my $password = $ARGV[2];
my $ftp = Net::FTP->new("$ftpsite") or
die "Cannot connect to $ftpsite : $@";
$ftp->login("$username", "$password") or
die "Cannot login : ", $ftp->message;
foreach my $dir (@{$db{subdir}}){
print "$filename -> $db{root}/$dir\n";
$ftp->put("$filename","$db{root}/$dir/$filename") or
die "put failed : ", $ftp->message;
print $ftp->message;
}
$ftp->quit();
Description of the codeuse Net::FTP; Load Net::FTP
my %db = (
'site' => "ftp.yoursite.com",
'root' => "subdir/cgi-bin",
'subdir' => [qw(
dir1
dir2
dir3
dir4
dir5
)],
);
FTP site, common directory from root directory, sub-directories to be uploaded in. In this example, the upload directory will be as follows; subdir/cgi-bin/dir1 subdir/cgi-bin/dir2 subdir/cgi-bin/dir3 subdir/cgi-bin/dir4 subdir/cgi-bin/dir5
die "Usage: $0 <filename> <username> <password>\n"
if ($#ARGV < 2);
If arguments are not correct, exit the script.
my $ftpsite = $db{site};
Put the FTP server name to a variable. my $filename = $ARGV[0]; my $username = $ARGV[1]; my $password = $ARGV[2]; Put arguments to variables.
my $ftp = Net::FTP->new("$ftpsite") or
die "Cannot connect to $ftpsite : $@";
Connect to the FTP site. At this point, login has not been done yet.
$ftp->login("$username", "$password") or
die "Cannot login : ", $ftp->message;
Pass username and password here and login. If failed to login, exit with a message.
foreach my $dir (@{$db{subdir}}){
print "$filename -> $db{root}/$dir\n";
$ftp->put("$filename","$db{root}/$dir/$filename") or
die "put failed : ", $ftp->message;
print $ftp->message;
}
Extract sub-directories one by one and upload $filename into the directory. $ftp->quit(); Quit FTP. If you want to change permission after uplaod, you can do as follows;
foreach my $dir (@{$db{subdir}}){
print "$filename -> $db{root}/$dir\n";
$ftp->put("$filename","$db{root}/$dir/$filename") or
die "put failed : ", $ftp->message;
print $ftp->message;
$ftp->site("chmod 755 $db{root}/$dir/$filename") or
die "site failed : ", $ftp->message;
print $ftp->message;
}
In this example, simply added the following lines;
$ftp->site("chmod 755 $db{root}/$dir/$filename") or
die "site failed : ", $ftp->message;
|