How can PHP be used to upload files to a different server using FTP?
To upload files to a different server using FTP in PHP, you can use the FTP functions provided by PHP. You need to establish an FTP connection to the remote server, login with the correct credentials, navigate to the desired directory, and then upload the file using the `ftp_put()` function.
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$file_path = "local_file.txt";
$remote_file_path = "/remote_directory/remote_file.txt";
// Connect to the FTP server
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
// Login to the FTP server
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);
// Change the current directory on the FTP server
ftp_chdir($ftp_conn, "/remote_directory");
// Upload the file to the FTP server
if (ftp_put($ftp_conn, $remote_file_path, $file_path, FTP_ASCII)) {
echo "File uploaded successfully";
} else {
echo "Error uploading file";
}
// Close the FTP connection
ftp_close($ftp_conn);
?>