【问题标题】:ftp_nlist returning an empty array for a non-empty directoryftp_nlist 为非空目录返回一个空数组
【发布时间】:2017-12-15 10:06:36
【问题描述】:

大家好,我有一个递归函数可以获取 ftp 目录和文件,但我无法获取某些文件夹的内容,例如测试文件夹 /public_html/test/myfiles.html 我没有得到 test 中的内容,否则该功能可以正常工作。

这是我的代码:

function ftpRecursiveFileListing($ftpConnection, $path, $parentId = null) {
static $cnt = 0;
$files = array();
$contents = ftp_nlist($ftpConnection, $path);

    foreach($contents as $currentFile) {
        $currentFileArray = array();
                $filesize = ftp_size($ftpConnection, $currentFile);
        $currentFileArray["text"] = $currentFile;
        $currentFileArray["parent_id"] = $parentId;
        $currentFileArray["id"] = $cnt++;
                $currentFileArray["filesize"] = NULL;
                if($filesize >= 0)
                    $currentFileArray["filesize"] = $filesize; // octets
        // assuming its a folder if there's no dot in the name
        if (strpos($currentFile, '.') === false)
            $currentFileArray["children"] = $this->ftpRecursiveFileListing($ftpConnection, $currentFile, $currentFileArray["id"]);
        $files [] = $currentFileArray;
    }
        return array_reverse($files) ;
    }

【问题讨论】:

  • 也许,只是一些子文件夹不是全部
  • 所以你对递归函数没有问题,对吧?您在列出几个特定目录时遇到问题。所以向我们展示一个简单的代码,它明确列出了一个有问题的目录。
  • 例如:我有一个文件夹“父”包含另一个文件夹“子”。当我从 / 根移动文件名“index.html”时。到 /parent/child/index.html 它工作正常我得到了结果但是当我将最后一个 /child/**index.html** 从 index.html 重命名为 default.html 或 yourname.html 我没有得到任何结果,这太疯狂了

标签: php recursion ftp


【解决方案1】:

尝试开启被动模式,就像在这篇文章中一样 ftp_nlist command not working

ftp_pasv($ftp_conn, true);
$files_list = ftp_nlist($ftp_conn, ".");

【讨论】:

  • OP 无法仅获取 一些 文件夹的内容。如果问题出在传输模式,OP 将无法检索任何文件夹。如果问题在于列表的转移,ftp_nlist 将返回 false,而不是“空数组”。
  • 我在 php.net 上发现以下关于 ftp_nlist 的评论可能会有所帮助......我注意到如果工作路径不同,此函数将不起作用,因为 $dir 保留整个路径字符串的值。这就是为什么添加 basename()-function 会去除路径,这个函数也可以在子文件夹中工作,而不仅仅是在根文件夹中。函数 ftp_is_dir($dir) { 全局 $ftp_connect; if (ftp_chdir($ftp_connect, basename($dir))) { ftp_chdir($ftp_connect, '..');返回真; } 否则 { 返回假; } }
【解决方案2】:

如果 tmp 文件夹中没有剩余空间,ftp_nlist 返回空数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2018-08-06
    相关资源
    最近更新 更多