What are the best practices for handling file uploads in PHP when the files need to be stored on a separate server?

When handling file uploads in PHP where the files need to be stored on a separate server, the best practice is to use a secure method like SFTP to transfer the files from the PHP server to the remote server. This ensures that the files are securely transferred and stored on the remote server without exposing any sensitive information.

// Example code for handling file uploads in PHP with SFTP to store files on a separate server

// Set up SFTP connection to remote server
$connection = ssh2_connect('remote-server.com', 22);
ssh2_auth_password($connection, 'username', 'password');

// Upload file to remote server
$localFile = '/path/to/local/file.txt';
$remoteFile = '/path/to/remote/file.txt';
ssh2_scp_send($connection, $localFile, $remoteFile, 0644);

// Close SFTP connection
ssh2_exec($connection, 'exit');