首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过PHP FTP上传整个目录

通过PHP FTP上传整个目录
EN

Stack Overflow用户
提问于 2009-05-29 18:05:47
回答 4查看 28.7K关注 0票数 16

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

我一直在浏览documentation on www.php.net,但似乎找不到一种方法一次上传一个以上的文件。

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

提前感谢您的帮助!

EN

回答 4

Stack Overflow用户

发布于 2009-05-29 18:25:51

打开连接后,按顺序上载目录的内容很简单:

代码语言:javascript
复制
foreach (glob("/directory/to/upload/*.*") as $filename)
    ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY);

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

票数 12
EN

Stack Overflow用户

发布于 2014-07-09 17:40:22

因此,我将@iYETER的代码包装为一个类对象。

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

代码语言:javascript
复制
$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();

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

它被封装到的类:

代码语言:javascript
复制
<?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;
}
}
票数 6
EN

Stack Overflow用户

发布于 2009-05-29 18:22:11

在循环中执行,遍历文件夹中的所有文件

代码语言:javascript
复制
$servername = $GLOBALS["servername"];
    $ftpUser = $GLOBALS["ftpUser"];
    $ftpPass = $GLOBALS["ftpPass"];
$conn_id = ftp_connect($servername) or die("<p style=\"color:red\">Error connecting to $servername </p>");

if(ftp_login($conn_id, $ftpUser, $ftpPass))
{
    $dir_handle = @opendir($path) or die("Error opening $path");

         while ($file = readdir($dir_handle)) {
            ftp_put($conn_id, PATH_TO_REMOTE_FILE, $file)
        }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/927341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档