【问题标题】:Php sort file from dir in desending order (Latest at top)Php 从 dir 按降序排序文件(最新在顶部)
【发布时间】:2014-12-12 18:00:03
【问题描述】:

**文件按日期降序排列,怎么做? ** 需要在顶部显示最新日期的文件

function list_dir($dn){
    if($dn[strlen($dn)-1] != '\\') $dn.='\\';
    static $ra = array();
    $handle = opendir($dn);
    while($fn = readdir($handle)){
        if($fn == '.' || $fn == '..') continue;
        if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\');
        else $ra[] = $dn.$fn;
    }
    closedir($handle);
    return $ra;
} 
 $filelist = list_dir('D:\xampp\htdocs');
    for($i=0;$i<count($filelist);$i++){
        $test = Array();
        $year = $test[2];
        $day = $test[1];
        $month = $test[0];       
        $test = explode("/",date("m/d/Y",filemtime($filelist[$i])));
        echo "<span style='color:red;'><b  style='color:green;'>".$day.'-'.month.'-'.$year. '</b> ' . $filelist[$i]."</span><br>";
    }
 clearstatcache();

【问题讨论】:

    标签: php file sorting dir


    【解决方案1】:

    你可以使用glob,然后arsort从高到低排序。

    $files = glob("*"); //Fetch all files.
    $files = array_combine($files, array_map("filemtime", $files)); //Grab the filetime for each file
    arsort($files); //Sort high to low
    echo "<pre>";
    echo print_r($files, true);
    echo "</pre>";
    

    输出(在表格中格式化后)

    <?php
    
    $files = glob("*"); //Fetch all files.
    $files = array_combine($files, array_map("filemtime", $files));
    arsort($files);
    echo "<table>
           <tr>
             <th>File</th>
             <th>Last modified</th>";
    foreach( $files as $file => $date ) {
        echo '<tr><td>'. $file .'</td><td>'. date("Y m d g:i:s a", $date).'</td></tr>';
    }
    echo "</table>";
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多