php - 检索大目录时PHP ftp_nlist返回false?
问题描述
我正在尝试做的事情: 使用 PHP ftp_nlist 检索 FTP 服务器上目录的内容
问题: 对于包含大量文件的目录(我遇到问题的目录有近 4 万个文件,没有子文件夹),ftp_nlist 函数返回 false。对于不是那么大的目录,ftp_nlist 函数按预期返回一个文件名数组。
我试过的:
- 启用被动模式(它已经启用,但我认为这是一个常见的建议)
- 添加 ftp_set_option($conn_id, FTP_USEPASVADDRESS, false); 在我的 ftp_login 之后
- 使用 ftp_chdir,虽然我的文件夹名称从来没有空格
- 在 ftp_nlist 返回 false 后回显 error_get_last()。错误显示似乎无关,但如下所示。
我的代码: 如果它有用,这是我创建的函数。它应该做的是...
- 接受 $fm (filemaker, 与这个问题无关)
- 接受 $FTPConnectionID(我在函数调用之前建立的 ftp 连接)
- 接受 $FolderPath(FTP 服务器上的文件夹路径,我想为其递归列出文件/子文件夹 - 例如:“SomeFolder/Testing”)
- 接受 $TextFile (我正在将 FTP 服务器上找到的每个文件的路径写入一个文本文件,该文件是在调用函数之前创建的)
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 问题。我已经坚持了几个小时,所以任何帮助表示赞赏。就像我说的,这个函数适用于大多数传递给它的文件夹路径,问题是当文件夹中有数以万计的文件时。谢谢!
解决方案
推荐阅读
- pandas - 将 xticks 设置为 df 列中的唯一值 - Seaborn
- reactjs - 自定义钩子和动态 useEffect 依赖
- border - |助推器 5| 边框颜色不变
- node.js - Discord Ouath2 访问令牌“不支持授予类型无”
- scheduled-tasks - 任务调度程序中的任务触发器
- angular - ng build --configuration 生产在 Jenkins 上需要 30 多分钟,但在本地需要 5-6 分钟
- java - 我想显示具有最高度量的行。但对我来说什么都没有
- reactjs - 无法在 react-lazyload 中将组件显示为占位符
- ios - 不符合协议 'Decodable' 和 'Encodable'
- angular - ngx-leaflet-markercluster spiderify 在大量标记中感到困惑和缓慢