Php ftp\u nlist为非空目录返回空数组
大家好,我有一个递归函数,可以获取ftp目录和文件,但是我不能获取一些文件夹的内容,例如测试文件夹 /public_html/test/myfiles.html我无法获取测试中的内容,否则该函数工作正常 这是我的代码:Php ftp\u nlist为非空目录返回空数组,php,recursion,ftp,Php,Recursion,Ftp,大家好,我有一个递归函数,可以获取ftp目录和文件,但是我不能获取一些文件夹的内容,例如测试文件夹 /public_html/test/myfiles.html我无法获取测试中的内容,否则该函数工作正常 这是我的代码: function ftpRecursiveFileListing($ftpConnection, $path, $parentId = null) { static $cnt = 0; $files = array(); $contents = ftp_nlist($ftpCon
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) ;
}
试着像这篇文章一样打开被动模式
ftp\u nlist返回空数组,如果tmp文件夹中没有剩余空间,则返回空数组可能的重复项,只是一些子文件夹并非全部,因此递归函数没有问题,对吗?您在列出几个特定目录时遇到问题。所以,给我们一个简单的代码,明确列出一个有问题的目录。例如:我有一个文件夹“Parent”包含另一个文件夹“Child”。当我从/root移动文件名“index.html”时。对于/parent/child/index.html,效果很好,我得到了结果,但当我将最后一个/child/**index.html**从index.html重命名为default.html或yourname.html时,我没有得到任何结果,这太疯狂了,无法仅获取某些文件夹的内容。如果问题是传输模式,OP将无法检索任何文件夹。另外,如果问题是列表的传输,则
ftp\u nlist
将返回false
,而不是“空数组”。我在php.net上找到以下关于ftp\nlist的注释可能会有所帮助。。。我注意到,若工作路径不同,该函数将不起作用,因为$dir保留了整个路径字符串的值。这就是为什么添加basename()-函数会剥离路径,并且该函数也会在子文件夹中工作,而不仅仅是在根文件夹中。函数ftp_是_dir($dir){global$ftp_connect;if(ftp_chdir($ftp_connect,basename($dir)){ftp_chdir($ftp_connect,…);返回true;}否则{return false;}
ftp_pasv($ftp_conn, true);
$files_list = ftp_nlist($ftp_conn, ".");