dongwu9170 2014-10-17 07:36
浏览 67
已采纳

来自dir的Php排序文件按降序排列(最新的)

**Files by date descending order, How to do it? ** Need to show Files by date latest at top

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();
  • 写回答

1条回答 默认 最新

  • dp926460 2014-10-17 07:43
    关注

    You can use glob, then arsort to sort from high to low.

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

    Output (after formatting in a table)

    http://i.imgur.com/SPgMH0q.png

    <?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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大