vashkatsi/folders-sync

使用 PHP 通过 HTTP 同步目录内容

1.0-beta 2013-05-24 00:14 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:52:43 UTC


README

PHP 7.1 代码重构

使用这些类递归同步不同服务器上两个文件夹的内容。源必须有网络服务器,但被同步的目录不需要是网络可访问的。客户端初始化连接,可以是另一个网络服务器或命令行脚本。

安装

如果使用 Composer,请将 "vashkatsi/folders-sync":"1.*@dev" 添加到您的需求中。

否则,只需下载并将类正常 require

工作原理

  1. 客户端收集目标文件夹(及子文件夹)中现有文件的列表,包括大小和修改日期
  2. 客户端将列表 POST 到服务器
  3. 服务器获取服务器上源文件夹中的文件列表,并与客户端的文件列表进行比较
  4. 服务器返回服务器上存在的新或修改过的文件列表
  5. 客户端请求每个新或修改过的文件的内容,并将其保存到目标文件夹
  6. 客户端将文件的最后修改时间设置为与服务器匹配

没有尝试发送差异;这不是 rsync。符号链接不支持。所有通信都通过请求/响应体中的 JSON 数据进行。

示例

在服务器上,例如 example.com/remote.php

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

在客户端

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

常见问题解答

为什么不用 rsync 呢?

有时你需要在各种托管环境中使代码可移植,因此不能依赖于 rsync、scp 或其他外部依赖。