ds3422222 2013-03-29 15:46
浏览 85
已采纳

使用PHP解析文件名并以HTML格式输出排序列表

Say I have a directory with the following files, named by date (month, day and year).

030313.pdf 030513.pdf 040113.pdf 052013.pdf

I know it is possible to break about the file names using explode() and I'm assuming I would need to save the data somehow to a multidimensional array like this:

$files = array
(
array("030313.pdf", 03,03,13),
array("030513.pdf", 03,05,13),
array("040113.pdf", 04,01,13),
array("052013.pdf", 05,20,13)
);

Is it possible to then take that information, group by month, sort by day and then echo that out as a bullet list of links?

Something like:

March 2013:

  • 03/03/2013 (linked to actual file)
  • 03/05/2013 (linked to actual file)

April 2013:

  • 04/01/2013 (linked to actual file)

May 2013:

  • 05/20/2013 (linked to actual file)

If this is possible, say there were 100-1000's of files, would loading this page cause any server performance issues?

Thanks

Brett

  • 写回答

2条回答 默认 最新

  • dsdvr06648 2013-03-29 16:24
    关注

    Here you go, just updated it to order them (I missed this when i first read your post.)

    <?php
    
    $files = array
    (
        array("030313.pdf", 03,03,13),
        array("030513.pdf", 03,05,13),
        array("040113.pdf", 04,01,13),
        array("052013.pdf", 05,20,13)
    );
    
    $newArray = array();
    
    
    foreach($files AS $file => $val){
            $newArray[$date = date('Ym', strtotime($val[1] . '/' . $val[2] . '/' . $val[3]))][] = $val[0];  
    }
    
    ksort($newArray);
    
    $list = '<ul>';
    foreach($newArray AS $key => $val){
    
        $list .= '<li>' . date('F Y', strtotime('01-' . substr($key, 4, 2) . '-' . substr($key, 0, 4)));
    
        if(is_array($val)){
            $list .= '<ul>';
            foreach($val AS $file => $filename){
    
                $list .= '<li><a href="' . $filename . '">Download ' . $filename . '</a></li>';
    
            }
            $list .= '</ul>';
        }
        $list .= '</li>';
    
    }
    
    $list .= '</ul>';
    
    echo $list;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)