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

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

This package is auto-updated.

Last update: 2024-09-15 20:30:45 UTC


README

使用这些类递归地同步不同服务器上两个文件夹的内容。源必须有一个Web服务器,尽管正在同步的目录不必是Web可访问的。客户端发起连接,可以是另一个Web服务器或命令行脚本。

安装

如果使用Composer,将 "outlandish/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 \Outlandish\Sync\Server(SECRET, PATH);
$server->run(); //process the request

在客户端(s)

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 \Outlandish\Sync\Client(SECRET, PATH);
$client->run('http://example.com/remote.php'); //connect to server and start sync

常见问题解答

为什么不直接使用rsync呢?

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