如何使用Perl通过FTP检查文件的大小?

时间:2009-11-18 07:28:02

标签: perl ftp

我有FTP Perl脚本,我想通过检查传输到远程服务器的字节数是否等于本地服务器中文件的实际字节来确定文件传输是否完成。我怎么能做到这一点?

这是我到目前为止所拥有的:

 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;

2 个答案:

答案 0 :(得分:6)

从文档中,您可以使用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.

代码:

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();

答案 1 :(得分:3)

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