通过 PHP FTP 上传整个目录

2022-08-31 01:04:17

我正在尝试编写一个脚本,该脚本将通过ftp将存储在我的服务器上的目录的全部内容上传到其他服务器。

我一直在阅读有关 www.php.net 的文档,但似乎找不到一次上传多个文件的方法。

有没有办法做到这一点,或者有没有一个脚本可以索引该目录并创建一个要上传的文件数组?

提前感谢您的帮助!


答案 1

打开连接后,批量上传目录的内容非常简单:

foreach (glob("/directory/to/upload/*.*") as $filename)
    ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY);

并行上传所有文件将更加困难。


答案 2

因此,我获取@iYETER的代码,并将其包装为类对象。

您可以通过以下几行调用此代码:

$ftp = new FtpNew("hostname");

$ftpSession = $ftp->login("username", "password");

if (!$ftpSession) die("Failed to connect.");

$errorList = $ftp->send_recursive_directory("/local/dir/", "/remote/dir/");
print_r($errorList);

$ftp->disconnect();

它以递归方式对本地 dir 进行爬网,并将其放在远程 dir 相对值上。如果它遇到任何错误,它会创建每个文件及其异常代码的数组层次结构(到目前为止我只捕获了2个,如果是另一个错误,它现在会抛出它的默认路由)

将 this 包装成以下类:

<?php
//Thanks for iYETER on http://stackoverflow.com/questions/927341/upload-entire-directory-via-php-ftp

class FtpNew {
private $connectionID;
private $ftpSession = false;
private $blackList = array('.', '..', 'Thumbs.db');
public function __construct($ftpHost = "") {
    if ($ftpHost != "") $this->connectionID = ftp_connect($ftpHost);
}

public function __destruct() {
    $this->disconnect();
}

public function connect($ftpHost) {     
    $this->disconnect();
    $this->connectionID = ftp_connect($ftpHost);
    return $this->connectionID;
}

public function login($ftpUser, $ftpPass) {
    if (!$this->connectionID) throw new Exception("Connection not established.", -1);
    $this->ftpSession = ftp_login($this->connectionID, $ftpUser, $ftpPass);
    return $this->ftpSession;
}

public function disconnect() {
    if (isset($this->connectionID)) {
        ftp_close($this->connectionID);
        unset($this->connectionID);
    }
}

public function send_recursive_directory($localPath, $remotePath) {
    return $this->recurse_directory($localPath, $localPath, $remotePath);
}

private function recurse_directory($rootPath, $localPath, $remotePath) {
    $errorList = array();
    if (!is_dir($localPath)) throw new Exception("Invalid directory: $localPath");
    chdir($localPath);
    $directory = opendir(".");
    while ($file = readdir($directory)) {
        if (in_array($file, $this->blackList)) continue;
        if (is_dir($file)) {
            $errorList["$remotePath/$file"] = $this->make_directory("$remotePath/$file");
            $errorList[] = $this->recurse_directory($rootPath, "$localPath/$file", "$remotePath/$file");
            chdir($localPath);
        } else {
            $errorList["$remotePath/$file"] = $this->put_file("$localPath/$file", "$remotePath/$file");
        }
    }
    return $errorList;
}

public function make_directory($remotePath) {
    $error = "";
    try {
        ftp_mkdir($this->connectionID, $remotePath);
    } catch (Exception $e) {
        if ($e->getCode() == 2) $error = $e->getMessage(); 
    }
    return $error;
}

public function put_file($localPath, $remotePath) {
    $error = "";
    try {
        ftp_put($this->connectionID, $remotePath, $localPath, FTP_BINARY); 
    } catch (Exception $e) {
        if ($e->getCode() == 2) $error = $e->getMessage(); 
    }
    return $error;
}
}

推荐