首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP FTP 遠程上傳類:解決主從服務器文件同步的輕量級方案

     編輯:PHP 綜合知識

設計特點

  • 專注上傳、刪除、讀取三大核心場景
  • 自動創建遠程目錄結構
  • 統一錯誤碼返回,便於調用方處理
  • 支持多服務器配置擴展
  • 被動模式適應防火牆/NAT 環境

核心類實現

class Ftp {
    private $testServer = [
        'host' => 'ip',
        'port' => 21,
        'user' => 'userName',
        'pwd'  => 'password'
    ];
    
    // 打開並登錄服務器
    public function openServer($flag = 'test') {
        $config = $this->getServerConfig($flag);
        $conn = ftp_connect($config['host'], $config['port']);
        if (!$conn) return 0; // 連接失敗
        if (!ftp_login($conn, $config['user'], $config['pwd'])) return 1; // 登錄失敗
        ftp_pasv($conn, true); // 被動模式
        return $conn;
    }
    
    // 遞歸創建目錄並定位
    public function makeDir($conn, $dirPath) {
        $dirPath = '/' . trim($dirPath, '/');
        $dirs = explode('/', $dirPath);
        foreach ($dirs as $dir) {
            if ($dir === '') $dir = '/';
            if (@ftp_chdir($conn, $dir) === false) {
                if (@ftp_mkdir($conn, $dir) === false) return 2; // 創建失敗
                @ftp_chdir($conn, $dir);
            }
        }
        return true;
    }
    
    // 上傳文件
    public function upload($flag = 'test', $local, $remote) {
        $conn = $this->openServer($flag);
        if (is_int($conn)) return $conn; // 0或1
        
        if ($this->makeDir($conn, dirname($remote)) === 2) return 2;
        
        $result = ftp_put($conn, basename($remote), $local, FTP_BINARY);
        $this->closeServer($conn);
        return $result ? 4 : 3; // 4成功 3失敗
    }
    
    // 刪除文件
    public function delete($flag = 'test', $remote) {
        $conn = $this->openServer($flag);
        if (is_int($conn)) return $conn;
        $result = ftp_delete($conn, $remote);
        $this->closeServer($conn);
        return $result ? 3 : 2;
    }
    
    // 讀取目錄列表
    public function read($flag, $remote) {
        $conn = $this->openServer($flag);
        if (is_int($conn)) return $conn;
        $list = ftp_nlist($conn, $remote);
        $this->closeServer($conn);
        return array_values(array_filter($list, fn($v) => !in_array($v, ['.', '..'])));
    }
    
    private function getServerConfig($flag) {
        return $this->testServer; // 可擴展多服務器
    }
    
    private function closeServer($conn) {
        if ($conn) ftp_close($conn);
    }
}

錯誤碼對照表

碼值含義處理建議
0服務器連接失敗檢查 IP/端口/防火牆
1服務器登錄失敗驗證用戶名密碼
2創建目錄失敗檢查父目錄權限
3操作失敗(上傳/刪除)檢查磁盤空間/權限
4操作成功正常流程

主從同步實戰場景

$ftp = new Ftp();

// 1. 主服務器生成靜態文件後推送到從服務器
$localFile = '/var/www/html/cache/page_' . $id . '.html';
$remoteFile = '/home/www/cache/page_' . $id . '.html';

$ret = $ftp->upload('slave1', $localFile, $remoteFile);
if ($ret !== 4) {
    // 記錄失敗,加入重試隊列
    log_sync_failure($id, $ret);
}

// 2. 內容刪除時同步清理
$ftp->delete('slave1', $remoteFile);

// 3. 定時校驗從服務器文件完整性
$remoteFiles = $ftp->read('slave1', '/home/www/cache/');
$localFiles = glob('/var/www/html/cache/*.html');
// 對比邏輯...

生產環境強化建議

  1. 連接復用:批量操作時複用連接,避免頻繁握手
  2. 異步隊列:將 FTP 操作推入 Redis/RabbitMQ,Worker 消費重試
  3. 校驗機制:上傳後對比 MD5/文件大小確保完整性
  4. 多從服務器:擴展 getServerConfig 支持陣列配置,並發推送
  5. 監控告警:同步失敗率、延遲指標接入 Prometheus/Grafana

總結

該類以 100 行左右代碼 解決主從文件同步核心需求,錯誤碼設計清晰,目錄遞歸創建健壯,是中小項目快速落地 FTP 同步的理想選擇。大規模場景建議對接 rsync/lsyncd 或對象存儲 CDN 方案。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved