Php 查找包含特定项的目录的最快方法
我有一个在WindowsXPSP3上运行的PHP5.3.4应用程序,我需要在远程PC上索引目录的内容。我索引的最大目录包含大约18000个项目 此呼叫将定位类似于Php 查找包含特定项的目录的最快方法,php,performance,Php,Performance,我有一个在WindowsXPSP3上运行的PHP5.3.4应用程序,我需要在远程PC上索引目录的内容。我索引的最大目录包含大约18000个项目 此呼叫将定位类似于\\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 = GetFil
\\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语法):
我不知道合适的windows shell命令(因为我的机器上安装了WinGnu32 grep),所以我无法帮助您
编辑:
我做了一些推敲,发现Windows相当于上面的命令:
dir /s /b | find "/bar/zoo.zip"
@Ivan-windows文件共享。(操作系统可能是Windows服务器的变体。我不确定是哪个…可能是2003服务器)
dir /s /b | find "/bar/zoo.zip"