2015-07-11 134 views
3

我已經在很多方面嘗試使用PHP中的FTP功能上傳文件。但它不適合我。我不知道我在這段代碼中犯了什麼錯誤。我已經給出了本地文件路徑或另一個服務器文件路徑,但在這兩種情況下它都不起作用。下面給出了我的代碼。任何人都可以幫助找到我的問題?使用php不工作將文件從一臺服務器上傳到另一臺服務器?

/* Source File Name and Path */ 
$backup_file = '/var/www/html/artbak/assets/uploads/edi/test.txt'; 
//$backup_file = '/workspace/all-projects/artbak/assets/uploads/edi/test.txt'; 
$remote_file = $backup_file; 

$ftp_host = 'hostname'; /* host */ 
$ftp_user_name = 'username'; /* username */ 
$ftp_user_pass = 'password'; /* password */ 

/* New file name and path for this file */ 
$local_file = '/public_html/example.txt'; 

/* Connect using basic FTP */ 
$connect_it = ftp_connect($ftp_host); 

/* Login to FTP */ 
$login_result = ftp_login($connect_it, $ftp_user_name, $ftp_user_pass); 

/* Download $remote_file and save to $local_file */ 
if (ftp_put($connect_it, $local_file, $remote_file, FTP_BINARY)) { 
    echo "WOOT! Successfully written to $local_file\n"; 
} 
else { 
    echo "Doh! There was a problem\n"; 
} 

/* Close the connection */ 
ftp_close($connect_it); 

以下代碼適用於本地到服務器的上載,但不適用於服務器到服務器的上載。它顯示服務器未連接。請給一些想法傢伙。我在這個概念上掙扎得更多。

$ftp_host = 'hostname'; /* host */ 
$ftp_user_name = 'username'; /* username */ 
$ftp_user_pass = 'password'; /* password */ 

// set up a connection or die 
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass); 

$file = "dummy.txt"; 

$remote_file = "receiveDummy.txt"; 

// upload a file 
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) { 
    echo "successfully uploaded $file\n"; 
} else { 
    echo "There was a problem while uploading $file\n"; 
} 

// close the connection 
ftp_close($conn_id); 
+0

給定的FTP詳細信息是目標服務器,它是正確的或我需要源服務器的詳細信息? –

+1

嘗試使用'error_reporting(E_ALL)'開啓錯誤,並在'else'分支中輸出'error_get_last()'。 – cmbuckley

+0

好吧,我會檢查它。錯誤顯示爲ftp_login(),ftp_put()和ftp_close()期望參數1爲資源,布爾給定。 –

回答

0

感謝您的支持人員。我很高興發佈這個答案几天的工作。只需要避開目標服務器路徑/public_html/test.txt而不是test.txt。如果你有任何子文件夾給像sample/test.txt。請檢查下面的代碼,對於像我這樣的人來說,它是完整的。

/* Source File Name and Path */ 
      $remote_file = '/var/www/html/artbak/assets/uploads/edi/test.txt'; 

      $ftp_host = 'hostname'; /* host */ 
      $ftp_user_name = 'username'; /* username */ 
      $ftp_user_pass = 'password'; /* password */ 

      /* New file name and path for this file */ 
      $local_file = 'test.txt'; 

      /* Connect using basic FTP */ 
      $connect_it = ftp_connect($ftp_host); 

      /* Login to FTP */ 
      $login_result = ftp_login($connect_it, $ftp_user_name, $ftp_user_pass); 

      /* Download $remote_file and save to $local_file */ 
      if (ftp_put($connect_it, $local_file, $remote_file, FTP_BINARY)) { 
       echo "WOOT! Successfully written to $local_file\n"; 
      } 
      else { 
       echo "Doh! There was a problem\n"; 
      } 

      /* Close the connection */ 
      ftp_close($connect_it); 

還有一件事是在我的服務器有一些防火牆模塊,因爲這個原因通過PHP代碼只有第一個FTP連接不。現在我也解決了這個問題。因爲我在本地服務器上的代碼已經知道了。爲此,我參考了下面的鏈接來解決服務器中的防火牆塊,Can't connect to FTP with PHP ftp_connect from localhost。所以它運作良好。

相關問題