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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题