【问题标题】:Get the latest file addition in a directory获取目录中最新的文件添加
【发布时间】:2009-09-29 07:08:33
【问题描述】:

如何获取最新的文件名,或者添加到目录中的文件路径?

【问题讨论】:

  • filectime 用于更改 chmod 值等元数据时。 filemtime 用于实际内容更改。

标签: php


【解决方案1】:
$path = "/path/to/my/dir"; 

$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;
  }
}

// now $latest_filename contains the filename of the file that changed last

【讨论】:

  • 有什么新方法可以快速搜索或扫描吗?
  • @Muhaimin Abdul 试试 scandir() ~ PHP 5
  • 正如其他人在其他答案中提到的那样......可能应该使用filemtime而不是filectime。 a) 跨平台得到更广泛的支持。 b)它返回你可能想要的:文件最后修改的时间
【解决方案2】:
$dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
$lastMod = 0;
$lastModFile = '';
foreach (scandir($dir) as $entry) {
    if (is_file($dir.$entry) && filectime($dir.$entry) > $lastMod) {
        $lastMod = filectime($dir.$entry);
        $lastModFile = $entry;
    }
}

【讨论】:

    【解决方案3】:
    $dir = "/path/to/Your/dir";         
    $pattern = '\.(zip|ZIP|pdf|PDF)$'; // check only file with these ext.          
    $newstamp = 0;            
    $newname = "";
    
    if ($handle = opendir($dir)) {               
           while (false !== ($fname = readdir($handle)))  {            
             // Eliminate current directory, parent directory            
             if (ereg('^\.{1,2}$',$fname)) continue;            
             // Eliminate other pages not in pattern            
             if (! ereg($pattern,$fname)) continue;            
             $timedat = filemtime("$dir/$fname");            
             if ($timedat > $newstamp) {
                $newstamp = $timedat;
                $newname = $fname;
              }
             }
            }
    closedir ($handle);
    
    // $newstamp is the time for the latest file
    // $newname is the name of the latest file
    // print last mod.file - format date as you like            
    print $newname . " - " . date( "Y/m/d", $newstamp); 
    

    【讨论】:

    • 这使用了一个折旧的函数:ereg()
    【解决方案4】:

    以下是使用DirectoryIterator 的方法:

    foreach(new DirectoryIterator('/path/to/read') as $item) {
        if ($item->isFile() && (empty($file) || $item->getMTime() > $file->getMTime())) {
            $file = clone $item;
        }
    }
    

    $file 的结果内容是 DirectoryIterator 类的一个实例,因此您可以访问它的所有方法。要简单地获取结果的完整路径,您可以这样做:

    echo $file->getPathname();
    

    【讨论】:

      【解决方案5】:

      我的 PHP 5 解决方案:

      $dir = "/path/to/Your/dir"; 
      $arraydir =scandir($dir, 1);
      echo "arraydir 0: " . $arraydir[0] . "<br/>"; // 1. new file
      echo "arraydir 1: " . $arraydir[1] . "<br/>"; // 2. new file
      echo "arraydir elements: " . count($arraydir) . "<br/>";
      

      (搜索词DE:neuste und zweitneuste Datei eines Ordners anzeigen mit PHP)

      【讨论】:

      • 按名称排序,仅当文件名中有按字母顺序排序的时间戳时才有效,例如exported_data_20170721.csv
      【解决方案6】:

      枚举所有目录文件,获取每个文件的filemtime()就完成了。

      【讨论】:

      • 我觉得你更需要filectime
      • @Gumbo:为什么使用 filectime 而不是 filemtime。我发现 filemtime() 在多种平台上都能更好地工作,而 filectime() 似乎无法在所有 Windows 服务器上正常工作。
      • @secretlm filectime 可能不起作用,因为它只是文件的创建时间,而不是修改时间
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2021-10-30
      • 2012-03-07
      • 2014-06-09
      • 2010-11-04
      相关资源
      最近更新 更多