查找以开头的最新文件

菲利普·LG

我希望使用共轭函数来查找以以下内容开头的最新文件:

$path = "/home/www/images/xml_cache"; 
$nom="images_album_6*.xml";

foreach (glob($path.'/'.$nom.'') as $filename) { }

  $latest_ctime = 0;
  $latest_filename = '';  
  $d = dir($path);
  while (false !== ($entry = $d->read( )))  {
    $filepath = "{$path}/{$entry}";
    // could do also other checks than just checking whether the entry is a file
    if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
      $latest_ctime = filectime($filepath);
      $latest_filename = $entry;
    }
  }
}

但是如何?

先感谢您

ring

假设您有PHP5,可以将RecursiveIterator类与getMTim​​e函数结合使用

$path = "/home/www/images/xml_cache"; 
$pattern = "/^images_album_6\S+.xml/i";
$latest_time = 0;
$latest_filename = '';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
    if ($file->isFile() && preg_match($pattern,$file->getFilename())) {
        if ($file->getMTime() > $latest_time) {
            $latest_time = $file->getMTime();
            $latest_filename = $file->getPathname();
        }
    }
}
print("Latest file: ".$latest_filename.PHP_EOL);

这将递归检查您指定的路径并打印与您的文件名模式匹配的最新文件的完整路径。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章