2009-11-18 91 views
1

我有FTP Perl腳本,我想通過檢查傳輸到遠程服務器的字節數是否等於本地服務器中文件的實際字節數來確認文件傳輸是否完成。我怎麼能做到這一點?如何使用Perl通過FTP檢查文件的大小?

這是我到目前爲止有:

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

$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; 

回答

6

從文檔,你可以使用大小()

 
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': [email protected]"; 

$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(); 
+0

這是好的,但我希望在傳輸文件之前將本地文件的大小和遠程文件的大小相匹配轉移。 我怎麼能這樣做? – Vijay 2009-11-18 08:37:56

+0

perldoc -f stat – ghostdog74 2009-11-18 09:07:08

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

如果不將ftp設置爲二進制模式,這將不起作用:$ ftp-> binary; – Fa11enAngel 2015-04-05 21:35:17