|
Transfer Files Between Servers |
|
|
|
Tutorials / FAQs -
Programming
|
|
If you've ever switched webhosts, you know how terribly frustrating it can be. One of the most painful tasks is uploading all the content to your new server from a slow home Internet connection. You can remove this bottleneck by transferring files between servers directly. Here I show you how to interact with FTP through PHP.
Recently someone on a forum I visit asked for tips about this: "I need a script that is capable of making a simple site mirroring, that is, to transfer one or more files directly between two servers without downloading them first to my local computer." Just swap out the comments if your situation calls for a file pull rather than a file push. FTP through PHP <?php $ftp_server = 'ftp.domain.com'; $ftp_user_name = 'ftpuser'; $ftp_user_pass = 'ftppass'; $server_file = '/path_on_ftp_server/filename.ext'; $local_file = '/path_to_webfolders/webdomain.com/filename.ext'; $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed.\n"; echo "Attempted to connect to $ftp_server for user $ftp_user_name.\n"; } else { echo "Connected to $ftp_server.\n"; } //UPLOAD $upload = ftp_put($conn_id, $server_file, $local_file, FTP_BINARY ); if ($upload) { echo "Uploaded $local_file to $ftp_server as $server_file.\n"; } else { echo "FTP upload has failed.\n"; } /* //DOWNLOAD $download = ftp_get($conn_id, $local_file, $server_file, FTP_BINARY); if ($download) { echo "Successfully written to $local_file\n"; } else { echo "There was a problem\n"; } */ ?>
|