当前位置: 首页> 最新文章列表> ftp_rawlist 获取递归目录结构的方法

ftp_rawlist 获取递归目录结构的方法

gitbox 2025-05-26

在使用 PHP 操作 FTP 的过程中,我们经常会遇到需要获取远程服务器上某个目录及其所有子目录和文件的情况。PHP 的 FTP 扩展中虽然提供了 ftp_nlistftp_rawlist 等方法,但它们默认只列出当前目录的内容。如果想要递归地获取完整的目录结构,就需要我们手动实现逻辑处理。本文将通过完整示例详细解析如何使用 ftp_rawlist 实现 FTP 上的目录结构递归获取。

基础知识回顾:ftp_rawlist

ftp_rawlist 是 PHP 中的一个函数,用于获取 FTP 服务器上指定目录的详细文件列表,它的返回结果类似于 UNIX 的 ls -l 命令输出的格式。例如:

Array
(
    [0] => drwxr-xr-x   2 ftp      ftp          4096 Jan 01 12:00 folder
    [1] => -rw-r--r--   1 ftp      ftp          1234 Jan 01 12:01 file.txt
)

我们可以从这些信息中解析出文件类型(目录或文件)、名称、大小、修改时间等信息。

实现思路

要实现递归获取目录结构,我们需要:

  1. 使用 ftp_rawlist 获取当前目录下的所有条目;

  2. 解析每一条的返回内容,识别出是目录还是文件;

  3. 如果是目录(并且不是 ...),则递归进入该目录;

  4. 将目录结构构建为树形数组,便于后续处理或展示。

完整示例代码

以下是一个可用的 PHP 示例,演示如何实现:

<?php

function getRecursiveFtpList($ftpConn, $path)
{
    $structure = [];
    $rawList = ftp_rawlist($ftpConn, $path);

    if ($rawList === false) {
        return $structure;
    }

    foreach ($rawList as $line) {
        $parts = preg_split("/\s+/", $line, 9);
        if (count($parts) < 9) continue;

        $name = $parts[8];
        $type = $parts[0][0];
        $fullPath = $path . '/' . $name;

        if ($type === 'd') {
            if ($name === '.' || $name === '..') continue;
            $structure[] = [
                'type' => 'directory',
                'name' => $name,
                'path' => $fullPath,
                'children' => getRecursiveFtpList($ftpConn, $fullPath),
            ];
        } elseif ($type === '-') {
            $structure[] = [
                'type' => 'file',
                'name' => $name,
                'path' => $fullPath,
                'size' => (int)$parts[4],
            ];
        }
    }

    return $structure;
}

// 示例用法:
$ftpHost = 'ftp.gitbox.net';
$ftpUser = 'your_username';
$ftpPass = 'your_password';

$conn = ftp_connect($ftpHost);
if (!$conn) {
    die("无法连接到 FTP 服务器");
}

if (!ftp_login($conn, $ftpUser, $ftpPass)) {
    ftp_close($conn);
    die("FTP 登录失败");
}

// 可选:设置被动模式
ftp_pasv($conn, true);

$directoryTree = getRecursiveFtpList($conn, '/');

ftp_close($conn);

// 打印结构(调试用)
echo "<pre>";
print_r($directoryTree);
echo "</pre>";

输出结构示例

运行上述代码后,你将得到如下类似的输出:

Array
(
    [0] => Array
        (
            [type] => directory
            [name] => public_html
            [path] => /public_html
            [children] => Array
                (
                    [0] => Array
                        (
                            [type] => file
                            [name] => index.php
                            [path] => /public_html/index.php
                            [size] => 1234
                        )
                    ...
                )
        )
    [1] => Array
        (
            [type] => file
            [name] => readme.txt
            [path] => /readme.txt
            [size] => 456
        )
)

注意事项

  1. 编码问题:某些 FTP 服务器返回的目录或文件名可能包含非 UTF-8 字符,需进行转码处理;

  2. 性能问题:递归操作在大目录结构下可能较慢,适当使用缓存或分页策略;

  3. 权限限制:确保 FTP 用户对递归目录具有读取权限,否则可能无法列出所有内容;

  4. 被动模式:有些 FTP 服务需要开启被动模式,使用 ftp_pasv($conn, true); 设置。

总结

通过 ftp_rawlist 配合递归处理,我们可以在 PHP 中完整获取 FTP 服务器上的目录结构。这种方法灵活性高,可以按需定制数据结构,对于自动化部署、备份同步、文件浏览等应用场景非常有用。

希望本文的完整示例能帮助你掌握这项技巧,并将其应用到实际开发中。