Smart Perl FTP Script

I was Working on a project required to use perl script to upload daily feed to a remote server. I wrote a small perl ftp script that I would like to share with caiapps’ vistors.

Here you go

#!/usr/bin/perl
use DBI;
use Net::FTP;
use warnings;

$myfile = $ARGV[0]; //passing file thru url
$ftpdomain = "yourftpdomain";
$ftpuser = "demo";
$ftppass = "p@ss";

$ftp = Net::FTP->new("$ftpdomain", Debug => 0) 
   or die("Couldn't connect to ftp server\n");
$ftp->login("$ftpuser",'$ftppass') or die "Couldn't login to ftp server\n";
$ftp->binary();

$local_file_size = -s $myfile;

&putfile(0); //start a fresh upload

sub putfile{
   $remote_file_size = $_[0];
   if ($remote_file_size != $local_file_size){
      $ftp->put($myfile) or die "Couldn't put ftp file: $myfile\n";
      $rsize = $ftp->size($myfile);
      &putfile($rsize);
   }
   else{
      print "file has been successfully uploaded";
   }
}

$ftp->quit;

Some notes

  1. It’s use perl module Net:FTP, check this perldoc for more infomation, http://perldoc.perl.org/Net/FTP.html, or thru command line, “perdoc NET:FTP”
  2. I did not use binary initially. Since the feed file is large (1.3M) , the file was uploaded imcompletly during the nightly cron, although it’s work everytime I manually run the script.
  3. Initially I were not using binary mode, and I found the file size of uploaded file is bigger than the size of uploading file even the file is uploaded successfully. The resaond could be extract link break, like “\r\n” being added.
  4. I use recursive function, the purpose is to make sure the size of uploaded file is the same as the size of uploading file, if not it will re-upload the file.