通过PHP FTP上传整个目录

Joe*_*per 16 php directory ftp upload

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

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

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

在此先感谢您的帮助!

Fra*_*mer 13

打开连接后,以串行方式上传目录的内容很简单:

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

并行上传所有文件会更加困难.


Xac*_*ery 5

所以,我拿了@ 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();
Run Code Online (Sandbox Code Playgroud)

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

这个课程包含在:

<?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;
}
}
Run Code Online (Sandbox Code Playgroud)


Bor*_*éry 0

如果您想一次上传多个文件,则需要使用 thread 或 fork。

我不确定 PHP 中是否有线程实现,但您应该看看PHP SPL和/或PEAR

编辑:感谢 Frank Farmer 让我知道 PHP 中有一个 fork() 函数,称为pcntl_fork()

您还必须递归地获取整个内容目录,以便能够上传给定目录的所有文件。