Uploading multiple files in multiple directories through FTP with one command

Top Japanese page




Overview

This 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 command

 putall.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

  1. Load Net::FTP
  2. Set directories to be copied into
  3. Put argument values into variables
  4. Connect to the FTP site and make an object
  5. Login to the FTP site
  6. Perform put command at each directory and upload the file
  7. Quit once all uploads are done

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 code

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