Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Synchronizing files between web servers

Wednesday, August 16th, 2017 by Servage

syncMany applications handle files in a way or another. Support tickets attachments, profile pictures, forum posts and many other features require files to be stored on servers. Often the easiest way is to store them on the filesystem of a web server. If your website consists of multiple web servers, you must send the uploaded files to the other servers as well to keep them synchronized. Here are a few suggestions on how you can achieve that.

Sending Files Using FTP

FTP is available on most web hosting environments. FTP is simple to use in PHP and supports a wide variety of commands for file handling.

Whenever one web server receives a file, you can use FTP to upload the file to another server. The first step with every FTP operation is to connect to an FTP server. We can do this using the ftp_connect() function. Here is an example: $ftp = ftp_connect(‘’). Next, we have to log in to the server: $loginResult = ftp_login($ftp, $username, $password). Finally, we can upload a file using the ftp_put() function: ftp_put($ftp, ‘name_on_remote_server.txt’, ‘/path/to/local/file.txt’, FTP_ASCII). The ftp_put() function returns true if the upload was successful and false if it failed.

With only a few lines of code we were able to upload a file to a remote server. Most of the time, basic attachment files are not modified once they have been uploaded to a web application. If your application allows users to modify attachments, you can simply reupload the file using the same commands as above. This will replace the file with the newest version.

Now we know how to add and modify files, and the last step is to delete them. The ftp_delete() function is perfect for that. As usually, we need a reference to an FTP connection using the ftp_connect() and ftp_login() functions. Then we can use the ftp_delete() method and pass a path to the file on the remote server as the second argument like this: ftp_delete($ftp, ‘/path/to/file.txt’).

SCP: An alternative to FTP

If FTP is not an option in your use case, doing the same things using SCP is possible. However, it should be noted that SCP is not available on web servers as commonly as FTP.

SCP works quite similarly to FTP in PHP. First, we must create an SCP connection: $scp = ssh2_connect(‘’). Next, we have to authenticate using either a password or an SSH key. This time we will use a password: ssh2_auth_password($scp, $username, $password).

Now we are ready to upload files: ssh2_scp_send($scp, ‘/path/to/local/file.txt’, ‘/path/to/remote/file.txt’). Like the FTP counterpart, this returns true on success and false on failure. To overwrite a file on the remote server, you can upload the file again.

SCP in PHP does not have a function to delete files from a remote server, but SFTP can be used to achieve that. If you are interested in a third alternative, have a look at the SFTP functions of PHP. Although the name might sound misleading, SFTP does not require FTP installed on a web server since it operates over SSH just like SCP.

Synchronizing files between web servers, 5.0 out of 5 based on 1 rating
Categories: Guides & Tutorials, Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.