我有一个包含 IP 地址和 unix TS 的文件列表。我需要获得一个去重的文件列表,但只有该主机的最新文件(这需要基于文件名中的 unix ts,而不是文件的实际 ts)。
样本数据:
192.168.1.6_1352405854.xml 192.168.1.6_1352408700.xml 192.168.1.6_1352409088.xml 192.168.189.14_1352409088.xml
192.168.1.6_1352407188.xml 192.168.1.6_1352408715.xml 192.168.1.6_1352409520.xml 192.168.189.14_1352409520.xml
192.168.1.6_1352407248.xml 192.168.1.6_1352408796.xml 192.168.1.6_1352409601.xml 192.168.189.14_1352409601.xml
192.168.1.6_1352407311.xml 192.168.1.6_1352408830.xml 192.168.1.6_1352412001.xml 192.168.189.14_1352412001.xml
192.168.1.6_1352407329.xml 192.168.1.6_1352408907.xml 192.168.1.6_1352415602.xml 192.168.189.14_1352415602.xml
192.168.1.6_1352408608.xml 192.168.1.6_1352409018.xml 192.168.189.14_1352409018.xml
所以预期的结果将是一个文件名数组,仅列出来自每个 IP 的最新文件。
就像是:
192.168.1.6_1352415602.xml
192.168.1.14_1352415602.xml
在我的代码中,我必须遍历目录结构并提取文件名和目录。
目录将是构成左侧菜单的站点名称。
这些文件将作为超链接主机名位于这些菜单项下(但我只需要来自每个主机的最新文件信息)
// Get directory list and (natural) sort it alphabetically (ignoring case)
$glob = "xml/vpnm/*";
$dirs = glob($glob,GLOB_ONLYDIR);
foreach ($dirs as $d){
$tmp[basename($d)] = basename($d);
}
natcasesort($tmp);
$dirs = array_keys($tmp);
foreach($dirs as $dir){
$dir = basename($dir);
$sitename = preg_replace('/_/', ' ', $dir);
echo "<h3><a href='#'>$sitename</a></h3>";
echo '<div>';
// Get list of hosts in the xml/ directory based on the file name
$year = date("Y");
$month = date("m");
$basepath = "xml/vpnm/$dir/$year/$month";
// note - preg_find is an include file, not a php function
$files = preg_find('/\.xml$/D', "xml/vpnm/$dir", PREG_FIND_RECURSIVE);
foreach ($files as $f){
// echo " F= $f \n";
$tmp[basename($f)] = basename($f);
}
natcasesort($tmp);
$files = array_keys($tmp);
natsort($files);
// do something here to deduplicate and only include the latest files.
foreach($files as $file) {
...