2009-08-17 34 views
1

我想通過Net::FTP和Perl上傳多個文件。有沒有人做過這件事,因爲即使我的基本腳本失敗了?爲什麼Perl的Net :: FTP不上傳我的文件?

use Net::FTP; 
use File::Basename; 

my $ftp; 
my $host ='56.309.24.2'; 
my $user ='user'; 
my $pw ='pass'; 

my $file ='097360718843.jpeg'; 
my $path ='public_html/uploaded/product_images'; 

chomp($host,$user,$pw,$path, $file); 

$ftp=Net::FTP->new($host) or die "could not login"; 
$ftp->login($user,$pw) or die "could not login"; 
$ftp->cwd($path) or die "could not cwd $path"; 
$ftp->ls; 
$ftp->put($file) or die "could not put $file"; 
$ftp->site("chmod 600 " . basename($file)); 

這裏有一個日誌轉移:

Net::FTP>>> 
Net::FTP(2.75) 
Net::FTP>>> Exporter(5.58) 
Net::FTP>>> 
Net::Cmd(2.26) 
Net::FTP>>> IO::Socket::INET(1.27) 
Net::FTP>>> IO::Socket(1.28) 
Net::FTP>>> IO::Handle(1.24) 
Net::FTP=GLOB(0x180c6a8)<<< 220---------- Welcome to Pure-FTPd [TLS] ---------- 
Net::FTP=GLOB(0x180c6a8)<<< 220-You are user number 1 of 50 allowed. 
Net::FTP=GLOB(0x180c6a8)<<< 220-Local time is now 16:19. Server port: 21. 
Net::FTP=GLOB(0x180c6a8)<<< 220-IPv6 connections are also welcome on this server. 
Net::FTP=GLOB(0x180c6a8)<<< 220 You will be disconnected after 15 minutes of inactivity. 
Net::FTP=GLOB(0x180c648)>>> user user_name 
Net::FTP=GLOB(0x180c648)<<< 331 User user_name OK. Password required 
Net::FTP=GLOB(0x180c648)>>> PASS .... 
Net::FTP=GLOB(0x180c648)<<< 230-User user_name has group access to: user_name wheel 
Net::FTP=GLOB(0x180c648)<<< 230 OK. Current restricted directory is/
Net::FTP=GLOB(0x180c648)>>> CWD public_html/uploaded/product_images/ 
Net::FTP=GLOB(0x180c648)<<< 250 OK. Current directory is /public_html/uploaded/product_images 
Net::FTP=GLOB(0x180c648)>>> PORT 192,168,1,10,200,38 
Net::FTP=GLOB(0x180c648)<<< 200 PORT command successful 
Net::FTP=GLOB(0x180c648)>>> NLST 
Net::FTP=GLOB(0x180c648)<<< 150 Connecting to port 50703 
Net::FTP=GLOB(0x180c648)<<< 226-Options: -a 
Net::FTP=GLOB(0x180c648)<<< 226 Output truncated to 2000 matches 
Net::FTP=GLOB(0x180c648)>>> ALLO 7903 
Net::FTP=GLOB(0x180c648)<<< 200 Zzz... 
Net::FTP=GLOB(0x180c648)>>> PORT 192,168,1,10,200,39 
Net::FTP=GLOB(0x180c648)<<< 200 PORT command successful 
Net::FTP=GLOB(0x180c648)>>> STOR 097360718843.jpeg 
Net::FTP=GLOB(0x180c648)<<< 150 Connecting to port 50704 
Net::FTP=GLOB(0x180c648)<<< 226-File successfully transferred 
Net::FTP=GLOB(0x180c648)<<< 226 0.381 seconds (measured here), 20.21 Kbytes per second 
Net::FTP=GLOB(0x180c648)>>> SITE chmod 600 097360718843.jpeg 
Net::FTP=GLOB(0x180c648)<<< 200 Permissions changed on 097360718843.jpeg 
+3

腳本失敗了嗎?或者腳本完成但不上傳文件? – chollida 2009-08-17 20:17:21

+0

它已完成但未上傳文件。 – 2009-08-17 20:21:25

+0

你是如何檢查文件已被上傳的?根據你發佈的調試日誌,它上傳的很好。 – 2009-08-17 21:07:36

回答

3

使用

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

其他好的做法:

  • 請確保您有

    use strict; 
    use warnings; 
    
  • 不需要在變量需要之前聲明變量。

  • 將錯誤包含在錯誤消息中。

總之,嘗試下面的腳本:

#!/usr/bin/perl 

use strict; 
use warnings; 

use Net::FTP; 

my $host ='56.309.24.2'; 
my $user ='user'; 
my $pw ='pass'; 

my $file ='097360718843.jpeg'; 
my $path ='public_html/uploaded/product_images'; 

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->put($file) 
    or die die sprintf "Could not login: %s", $ftp->message; 

$ftp->site("chmod 600 $file"); 
+0

好吧,我有一些輸出。我登錄到服務器,以便連接,但沒有上傳。 – 2009-08-17 20:21:58

+0

@unknown(google):您應該提供調試輸出。 – 2009-08-17 20:24:19

+0

Net :: FTP >>> Net :: FTP(2.75) Net :: FTP >>> Exporter(5.58) Net :: FTP >>> Net :: Cmd(2.26) Net :: FTP >>> IO :: Socket :: INET(1.27) Net :: FTP >>> IO :: Socket(1.28) Net :: FTP >>> IO :: Handle(1.24) Net :: FTP = GLOB(0x180c6a8) <<< 220 ----------歡迎使用Pure-FTPd [TLS] ---------- Net :: FTP = GLOB(0x180c6a8)<<< 220-您是用戶允許的第一個50。 Net :: FTP = GLOB(0x180c6a8)<<< 220 - 當地時間現在是16:19。服務器端口:21. Net :: FTP = GLOB(0x180c6a8)<<<此服務器上也歡迎220-IPv6連接。 Net :: FTP = GLOB(0x180c6a8)<<< 220您將在15分鐘不活動後斷開連接。 – 2009-08-17 20:31:17

1

檢查目標目錄的保護。 確保你可以在那裏創建文件。

檢查文件本身的保護 確保您可以覆蓋它們。

+0

那麼,它沒有實際上傳文件,因爲我無法在目錄中看到它。我可以在這個目錄中創建文件,並將文件上傳到第三方ftp程序中。我真的不知道爲什麼會發生這種情況。 – 2009-08-17 23:58:44

0

上傳成功:

226-File successfully transferred 

,它應該是通過在Web瀏覽器中的文件URL訪問。然而,你不能看到它在一個FTP客戶端,因爲FTP服務器文件中列出的限制 - 文件數量超過2000:

226 Output truncated to 2000 matches 

該解決方案將被保存到其他文件夾,或者如果你有機會獲得服務器端的ftp配置變化該文件的限制。