What potential issues can arise when transferring files between servers using FTP in PHP?

One potential issue when transferring files between servers using FTP in PHP is the lack of error handling, which can result in failed transfers going unnoticed. To solve this, it is important to implement proper error checking and handling mechanisms to ensure the success of the file transfer.

// Connect to the FTP server
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$ftp_connection = ftp_connect($ftp_server);
$login = ftp_login($ftp_connection, $ftp_username, $ftp_password);

// Check if connection was successful
if (!$ftp_connection || !$login) {
    die('FTP connection failed');
}

// Enable passive mode
ftp_pasv($ftp_connection, true);

// Transfer file
$file_to_transfer = 'file.txt';
$remote_file_path = '/path/to/remote/file.txt';
if (ftp_put($ftp_connection, $remote_file_path, $file_to_transfer, FTP_BINARY)) {
    echo 'File transferred successfully';
} else {
    echo 'Failed to transfer file';
}

// Close connection
ftp_close($ftp_connection);