Unable to get the size of remote file using Net::FTP Perl Script

Hi,

I am using below piece of code to get the size of the remote file.


            $ftp->cwd($destination) or $error=$ftp->message;
            if(!$error)
            {
                $ftp->put($file) or  $error=$ftp->message;
                print "FTP size = [", $ftp->size($file), "] \n";
                print "Local size = [", (-s $file), "] \n";
                &log($failure_logfile,$file, "Error in copying file to destination ".$error) if($error);
                &log($success_logfile,$file,"Success") if(!$error);


Output is:

FTP size = []
Local size = [1992]

Need your help on this.

Thanks,
Farooq

Where does $ftp come from?

What is this '&log'?

Why not just post your code?

Here is the entire code:

#! /usr/bin/perl
use Net::FTP;
my  $ipadd    = $ARGV[0];
my  $user     = $ARGV[1];
my  $password = $ARGV[2];
my  $destination = $ARGV[3];
my $file      = $ARGV[4];
my $success_logfile='Success';
my $failure_logfile='Failure';
my $error;
if(($ipadd) && ($user) && ($password) && ($destination) && ($file))
{
  my $ftp = Net::FTP->new($ipadd) or $error="Cannot connect to $ipadd: $@";
  if(!$error)
  {
     $ftp->login($user,$password) or $error=$ftp->message;
     if(!$error)
     {
         if(-e $file)
         {
            $ftp->binary();

            $ftp->cwd($destination) or $error=$ftp->message;
            if(!$error)
            {
                $ftp->put($file) or  $error=$ftp->message;
                print "FTP size = [", $ftp->size($file), "] \n";
                print "Local size = [", (-s $file), "] \n";
                &log($failure_logfile,$file, "Error in copying file to destination ".$error) if($error);
                &log($success_logfile,$file,"Success") if(!$error);
            }
         }
     }
     $ftp->quit;
  }
}

sub log
{
    my $log_file        = shift;
    my $filename        = shift;
    my $result = shift;
    $log_file="log/".$log_file;
    if (!(-e "$log_file.csv"))
    {
         open  (FL,">$log_file.csv")||die "Cannot open Log File.$! \n";
         print FL "Filename,Result\n";
         close (FL);
    }
    open (LOG,">>$log_file.csv") || die "Cannot open Log File.$! \n";
    print LOG "$filename,$result \n";
    close (LOG);
}

That Net::FTP works at all is a minor miracle, FTP is extremely hard to script, with very different output between servers. There's no standard layout for ls in ftp for example, meaning some ftp servers may not show the size at all.

If it succeeded in uploading the file, the size isn't going to be different IMHO. (Unless you didn't put FTP into binary mode!) If something happened, the FTP connection would probably break completely.