What is the best way to save a file uploaded via a form to a different server using PHP?

When saving a file uploaded via a form to a different server using PHP, you can achieve this by first moving the uploaded file to a temporary location on the current server, then using PHP's FTP functions to transfer the file to the remote server. This approach ensures that the file is securely uploaded and transferred to the desired server.

<?php
// Get the uploaded file
$uploadedFile = $_FILES['file']['tmp_name'];

// Move the uploaded file to a temporary location on the current server
$tempFile = 'temp/' . $_FILES['file']['name'];
move_uploaded_file($uploadedFile, $tempFile);

// Connect to the remote server via FTP
$ftpServer = 'ftp.example.com';
$ftpUsername = 'username';
$ftpPassword = 'password';
$ftpConnection = ftp_connect($ftpServer);
ftp_login($ftpConnection, $ftpUsername, $ftpPassword);

// Transfer the file to the remote server
ftp_put($ftpConnection, '/remote/path/' . $_FILES['file']['name'], $tempFile, FTP_BINARY);

// Close the FTP connection
ftp_close($ftpConnection);

// Clean up temporary file
unlink($tempFile);
?>