[英]Upload files from one server to another server using php not working?
我已經嘗試過多種方式使用PHP中的FTP函數上傳文件。 但這對我不起作用。 我不知道我在這段代碼中犯了什么錯誤。 我也給了本地文件路徑或其他服務器文件路徑,但在這兩種情況下它都不起作用。 我在下面給出了我的代碼。 任何人都可以幫助找到我的問題?
/* 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);
謝謝你們的支持。 我很高興發布這個答案幾天的工作。 只需要避免使用目標服務器路徑/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 );
有一件事是在我的服務器上有一些防火牆塊,因此只有第一個ftp沒有通過PHP代碼連接。 現在我也解決了這個問題。 因為我在本地工作的代碼已經知道了。 為此,我引用了以下鏈接來解決服務器中的防火牆塊, 無法使用來自localhost的PHP ftp_connect連接到FTP 。 所以它運作良好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.