2014-10-28 74 views
3

現在默認情況下,它顯示的字母表我不想那樣。我想通過使用RecursiveDirectoryIterator頂部的最新文件來排序文件。按降序排列。按日期排序文件最新在頂部使用RecursiveDirectoryIterator

還可以使用如果條件從該日起比較日期&獲取文件

<?php 
    $search_path = 'D:\xampp\htdocs'; 
    $file_extension = 'php'; 
    $it = new RecursiveDirectoryIterator("$search_path"); 
    $display = Array ($file_extension); 


    foreach(new RecursiveIteratorIterator($it) as $file) { 
     $test = Array(); 
     $test = explode("/",date("m/d/Y",filemtime($file))); 
     $year = $test[2]; 
     $day = $test[1]; 
     $month = $test[0]; 


    if (in_array(strtolower(array_pop(explode('.', $file))), $display)) 
      if(($year >= 2014) && ($month >= 1) && ($day >= 15)){ 
        echo "<span style='color:red;'>"; 
        echo "<b style='color:green;'>".$day.'-'.$month.'-'.$year. '</b> ' . $file."</span><br>"; 
      } 
     } 

    ?> 

回答

2

我不知道你是否能進行排序直通的DirectoryIterator通過直接,你可以先收集結果,獲取時間然後排序,然後呈現它。例如:

$display = array('php'); 
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($search_path)); 
$data = array(); 
foreach($files as $file) { 
    $time = DateTime::createFromFormat('U', filemtime($file->getPathname())); 
    // no need to explode the time, just make it a datetime object 
    if(in_array($file->getExtension(), $display) && $time > new DateTime('2014-01-15')) { // is PHP and is greater than jan 15 2014 
     $data[] = array('filename' => $file->getPathname(), 'time' => $time->getTimestamp()); // push inside 
    } 

} 
usort($data, function($a, $b){ // sort by time latest 
    return $b['time'] - $a['time']; 
}); 


foreach ($data as $key => $value) { 
    $time = date('Y-m-d', $value['time']); 
    echo " 
     <span style='color: red;'> 
      <b style='color: green;'>$time</b>$value[filename] 
     </span> 
     <br/> 
    "; 
} 
+0

謝謝你,完美的作品。 我實際上在數據項目上存儲了另一個參數,所以我可以按此排序。 – 2017-12-06 09:55:26