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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测