【问题标题】:PHP - how to get most recent file from directory AND subdirectoriesPHP - 如何从目录和子目录中获取最新文件
【发布时间】:2012-03-11 10:22:02
【问题描述】:

我已使用以下脚本正确显示所选目录及其子目录中的所有文件。有谁知道如何修改此代码仅在目录/子目录中回显最新文件?

函数 ListFiles($dir) {

    if($dh = opendir($dir)) {

        $files = 数组();
        $inner_files = 数组();

        而($file = readdir($dh)) {
            if($file != "." && $file != ".." && $file[0] != '.') {
                if(is_dir($dir . "/" . $file)) {
                    $inner_files = ListFiles($dir . "/" . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files);
                } 别的 {
                    array_push($files, $dir . "/" . $file);
                }
            }
        }

        关闭($dh);
        返回$文件;
    }
}


foreach (ListFiles('media/com_form2content/documents/c30') as $key=>$file){
    echo "{aridoc engine=\"google\" width=\"750\" height=\"900\"}" 。 $file ."{/aridoc}";
}

【问题讨论】:

    标签: php file filesystems subdirectory


    【解决方案1】:

    PHP5你可以使用RecursiveDirectoryIterator递归扫描一个目录下的所有文件:

    $mostRecentFilePath = "";
    $mostRecentFileMTime = 0;
    
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("YOURDIR"), RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile()) {
            if ($fileinfo->getMTime() > $mostRecentFileMTime) {
                $mostRecentFileMTime = $fileinfo->getMTime();
                $mostRecentFilePath = $fileinfo->getPathname();
            }
        }
    }
    

    【讨论】:

    • 感谢您的快速回复,但我需要跨目录和子目录获取最新文件。如何修改上面的代码?
    • @bjornkock 所以你想在一个目录和它的子目录中获取最近 mtime 的文件?
    【解决方案2】:

    您可以使用filemtime() 检索文件最后修改的unix 时间戳。

    【讨论】:

      【解决方案3】:

      您可以使用它来获取目录中的最后一个添加文件

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

      【讨论】:

        【解决方案4】:

        你可以试试这个

        $last_mtimes = array(); 
        function ListFiles($dir) {
        
        if($dh = opendir($dir)) {
        
        $files = Array();
        $inner_files = Array();
        
        while($file = readdir($dh)) {
            if($file != "." && $file != ".." && $file[0] != '.') {
                if(is_dir($dir . "/" . $file)) {
                    $inner_files = ListFiles($dir . "/" . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else {
                    array_push($files, $dir . "/" . $file);
                     $lmtime = filemtime($dir . "/" . $file) ;
                     $last_mtimes[$lmtime] = $dir . "/" . $file;
                }
            }
        }
         // now ksort your $last_mtimes array
        krsort($last_mtimes);
        // either return this array or do whatever with the first val
        closedir($dh);
        return ($last_mtimes);
        
        }
        }
        // prints in decsending order
        foreach (ListFiles('PATH_TO_YOUR_DIRECTORY') as $key=>$file){
        echo "{aridoc engine=\"google\" width=\"750\" height=\"900\"}" . $key."=>".$file ."  {/aridoc}";
         }
         // prints last modified files
         echo array_shift(ListFiles('YOUR_DIRECTORY_PATH'));
        

        希望对你有帮助

        【讨论】:

        • 感谢您的快速回复,但这似乎不起作用。
        • print_r($last_mtimes); 打印了什么。我很好奇为什么它不起作用。做了一个小编辑再试一次。请注意,我不会返回 $last_mtimes 数组
        • print_r($last_mtimes) 返回: Array ( [1329271076] => logo_lg.jpg ) Array ( [1327908278] => H1B application 2009.pdf ) Array ( [1329271671] => Employee Manual.pdf ) 数组 ( [1322843201] => Company.docx ) 数组 ( [1327909484] => prop01-06.JPG )
        • 但我只需要最新的文件。
        • 上面的代码似乎可以工作,但它会按照各自最后修改日期的降序列出所有文件。如果您愿意,您可能希望在第一次迭代后打破 for 循环。或使用$most_recent_file = array_shift(ListFiles('YOUR_DIRECTORY_PATH')); 获取最近修改的文件
        【解决方案5】:

        我建议你使用filemtime() 函数。

        这将为您提供文件的最后修改日期。

        【讨论】:

          猜你喜欢
          • 2021-10-30
          • 1970-01-01
          • 1970-01-01
          • 2019-08-14
          • 1970-01-01
          • 2012-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多