首页 > 解决方案 > 检索大目录时PHP ftp_nlist返回false?

问题描述

我正在尝试做的事情: 使用 PHP ftp_nlist 检索 FTP 服务器上目录的内容

问题: 对于包含大量文件的目录(我遇到问题的目录有近 4 万个文件,没有子文件夹),ftp_nlist 函数返回 false。对于不是那么大的目录,ftp_nlist 函数按预期返回一个文件名数组。

我试过的:

我的代码: 如果它有用,这是我创建的函数。它应该做的是...

function createAuditFile($fm, $FTPConnectionID, $FolderPath, $TextFile) {
    echo "createAuditFile called for " . $FolderPath . "\n";
    //Get the contents of the given path.  Will include files and folders.
    $FolderContents = ftp_nlist($FTPConnectionID, $FolderPath);
    if($FolderContents == false) {
        echo "Couldn't get " . $FolderPath . "\n";
        echo "ERROR: " . print_r(error_get_last()) . "\n";
    } else {
        print_r($FolderContents);
    }
        
    //Loop through the array, call this function recursively if a folder is found.
    
    if(is_array($FolderContents)) {
        foreach($FolderContents as $Content) {
            //Create a varaible for the folder path
            $ContentPath = $FolderPath . "/" . $Content;

            //Call the function recursively if a folder is found
            if(pathinfo($Content, PATHINFO_EXTENSION) == "") {
                createAuditFile($fm, $FTPConnectionID, $ContentPath, $TextFile);
                echo "Recursive call for " . $ContentPath . "\n";
            //If a file is found, add the file ftp path to our array
            } else {
                echo "Writing to file: " . $ContentPath . "\n";
                fwrite($TextFile, $ContentPath . "\n");
            }

        }
    }
    
}

如果需要,我可以提供其他代码,但我认为我的问题与其说是编码问题,不如说是理解 ftp_nlist 问题。我已经坚持了几个小时,所以任何帮助表示赞赏。就像我说的,这个函数适用于大多数传递给它的文件夹路径,问题是当文件夹中有数以万计的文件时。谢谢!

标签: phpdirectoryftp

解决方案


推荐阅读