Sync directory contents over HTTP using PHP
PHP 7.1 code refactored, (*1)
Use these classes to recursively sync the contents of two folders on different servers. The source must have a web server although the directory being synced does not have to be web accessible. The client initiates the connection and can be either another web server or a command line script., (*2)
If using Composer, add "vashkatsi/folders-sync":"1.*@dev"
to your requirements., (*3)
Otherwise, just download and require
the classes as normal., (*4)
No attempt is made to send diffs; this is not rsync. Symlinks are not explicitly supported. All communication is via JSON data in the request/response body., (*5)
On the server, e.g. example.com/remote.php
:, (*6)
require_once 'vendor/autoload.php'; //or include AbstractSync.php and Server.php const SECRET = '5ecR3t'; //make this long and complicated const PATH = '/path/to/source'; //sync all files and folders below this path $server = new \Vashkatsi\Sync\Server(SECRET, PATH); $server->run(); //process the request
On the client(s):, (*7)
require_once 'vendor/autoload.php'; const SECRET = '5ecR3t'; //this must match the secret key on the server const PATH = '/path/to/destination'; //target for files synced from server $client = new \Vashkatsi\Sync\Client(SECRET, PATH); $client->run('http://example.com/remote.php'); //connect to server and start sync
Sometimes you need code to be portable across a range of hosting environments so you can't rely on rsync, scp or other external dependencies., (*8)