How can PHP be used to transfer files between different servers using FTP functions?

To transfer files between different servers using FTP functions in PHP, you can use the built-in FTP functions provided by PHP. These functions allow you to connect to an FTP server, upload files, download files, and perform other FTP operations. By using functions like ftp_connect, ftp_login, ftp_put, and ftp_get, you can easily transfer files between servers.

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

// Check connection
if ((!$conn_id) || (!$login_result)) {
    die('FTP connection has failed!');
}

// Transfer file
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
    echo "Successfully uploaded $local_file to $ftp_server/$remote_file\n";
} else {
    echo "There was a problem while uploading $local_file\n";
}

// Close connection
ftp_close($conn_id);