使用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

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;

?>

The solution is very simple:

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

$sortedData = array();

foreach ($files as $file) {
    $dt = mktime(0, 0, 0,  $file[1], 1, 2000);
    $key = date('F', $dt) . ' ' . $file[3];
    if (array_key_exists($key, $sortedData)) {
        $sortedData[$key] []= $file;
    } else {
        $sortedData[$key] = array($file);
    }
}


function sortFunc($a, $b) {
    if ($a[2] > $b[2]) {
        return 1;
    } else if ($a[2] < $b[2]) {
        return -1;
    } 
    return 0;
}

foreach ($sortedData as &$sd) {
    usort($sd, sortFunc);
}


foreach ($sortedData as $key => $data) {
    echo "<h1>".$key."</h1>
";
    foreach ($data as $d) {
        echo $d[0]."
";
    }
}

Just reformat the output.

Performance depends mostly on filesystem, used on server. Reformating the array of 1000 elemens usually is nothing.