简体   繁体   English

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

[英]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. 我有FTP Perl脚本,我想通过检查传输到远程服务器的字节数是否等于本地服务器中文件的实际字节数来确保文件传输是否完成。 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;

from the docs, you can use size() 从文档中,您可以使用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();
print "FTP size = ", $ftp->size($file), "\n";
print "Local size = ", (-s $file), "\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM