Vijay
Vijay

Reputation: 67221

How can I check the size of a file over FTP using Perl?

I have FTP Perl script and I want to make sure whether the file transfer is complete by checking the number of bytes that are transfered to the remote server is equal to the actual bytes of the file in the local server. How could i accomplish this?

Here's what I have so far:

 my $ftp = Net::FTP->new($host, Debug => 1)
 or die "Could not connect to '$host': $@";

 $ftp->login($user, $pw)
 or die sprintf "Could not login: %s", $ftp->message;

 $ftp->cwd($path)
 or die sprintf "Could not login: %s", $ftp->message;

 $ftp->ls;

 $ftp->binary;

 $ftp->get($file)
 or die sprintf "Could not login: %s", $ftp->message;

Upvotes: 1

Views: 5887

Answers (2)

ghostdog74
ghostdog74

Reputation: 342373

from the docs, you can use size()

size ( FILE )

Returns the size in bytes for the given file as stored on the remote server.

NOTE: The size reported is the size of the stored file on the remote 
server. If the file is subsequently transferred from the server in ASCII
mode and the remote server and local machine have different ideas about
"End Of Line" then the size of file on the local machine after transfer
may be different.

Code:

my $host="127.0.0.1";
my $user="anonymous";
my $pw = "asdfsf";
my $path="pub";
my $file="file";
my $ftp = Net::FTP->new($host, Debug => 0) 
    or die "Could not connect to '$host': $@";

$ftp->login($user, $pw) or die sprintf "Could not login: %s", $ftp->message;
$ftp->cwd($path) or die sprintf "Could not login: %s", $ftp->message;
$ftp->binary;
print $ftp->size($file) or die sprintf "Could not login: %s", $ftp->message;
$ftp->quit();

Upvotes: 6

user213617
user213617

Reputation:

print "FTP size = ", $ftp->size($file), "\n";
print "Local size = ", (-s $file), "\n";

Upvotes: 3

Related Questions