我有一个在 Windows XP SP3 上运行的 PHP 5.3.4 应用程序,我需要对远程 PC 上的目录内容进行索引。我正在索引的最大目录包含大约 18,000 个项目。
此调用将查找诸如 \\somepc.mycorp.com\foo\mydir\bar\zoo.zip
之类的项目。
// look in all the directories in \\somepc.mycorp.com\foo for directories containing a file \bar\zoo.zip
$item_list = GetFileList('\\\\somepc.mycorp.com\\foo', '\\bar\\zoo.zip');
其实现为:
function GetFileList($base_dir, $path_mask)
{
$result= array();
if ($handle = opendir($base_dir))
{
while (false !== ($entry = readdir($handle)))
{
// only add items that match the mask we're looking for
if ($entry != "." &&
$entry != ".." &&
file_exists($base_dir.'\\$entry\\$path_mask'))
{
array_push($result, $entry);
}
}
closedir($handle);
}
return $result;
}
不幸的是,对于最大的目录结构,此操作可能需要一个多小时。如果我删除过滤器并将看到的每个项目插入到数组中,它将在几秒钟内完成。
有没有更快的方法来实现这一点?
最佳答案
我讨厌拉这张卡,但是使用 bash 甚至 Windows 脚本可以更快地完成您正在做的事情 - PHP 可能不是适合这里工作的工具。对 dir/s/b
或 find
的系统调用(甚至在 PHP 中)将获得所有存在文件的列表,然后您可以迭代这些字符串使用 PHP 比检查每个目录中是否存在文件要快得多。
我会在 bash 中这样做(因为我很懒并且不知道正确的查找语法):
find | grep '/bar/zoo.zip'
我不知道适当的 Windows shell 命令(因为我的计算机上安装了 WinGnu32 grep),所以我无法帮助您。
编辑:
我做了一些修改,发现了与上述命令等效的 Windows 版本:
dir /s /b | find "/bar/zoo.zip"
关于php - 查找包含特定项目的目录的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8852985/