我已經在很多方面嘗試使用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);
給定的FTP詳細信息是目標服務器,它是正確的或我需要源服務器的詳細信息? –
嘗試使用'error_reporting(E_ALL)'開啓錯誤,並在'else'分支中輸出'error_get_last()'。 – cmbuckley
好吧,我會檢查它。錯誤顯示爲ftp_login(),ftp_put()和ftp_close()期望參數1爲資源,布爾給定。 –