从文件夹中取出文件并按类型进行排序


take files from folder and sort them by type

需要帮助,我有这个文件结构:

文件(文件夹).pdf.pdf.doc.doc.doc拉链.zip
index.php

我想在index.php中显示"files"文件夹中的文件,并按类型对它们进行排序。此外,我想这样显示:

对于zip:

<div class="dopobrania_zip">
    <a href="file adress">file title</a>
</div>

pdf格式:

<div class="dopobrania_pdf">
    <a href="file adress">file title</a>
</div>

对于文档:

<div class="dopobrania">
    <a href="file adress">file title</a>
</div>

你能帮我吗?

你应该试试这段代码,也许你能找到出路。

//directory to list, you choose; here we’ll just use the webroot
$path = $_SERVER['DOCUMENT_ROOT'];
//warning: `is_dir` will need you to change to the parent directory of what you are testing
//see <uk3.php.net/manual/en/function.is-dir.php#70005> for details
chdir ($path);
//get a directory listing
$dir = array_diff (scandir ('.'),
    //folders / files to ignore
    array ('.', '..', '.DS_Store', 'Thumbs.db')
);
//sort folders first, then by type, then alphabetically
usort ($dir, create_function ('$a,$b', '
    return  is_dir ($a)
        ? (is_dir ($b) ? strnatcasecmp ($a, $b) : -1)
        : (is_dir ($b) ? 1 : (
            strcasecmp (pathinfo ($a, PATHINFO_EXTENSION), pathinfo ($b, PATHINFO_EXTENSION)) == 0
            ? strnatcasecmp ($a, $b)
            : strcasecmp (pathinfo ($a, PATHINFO_EXTENSION), pathinfo ($b, PATHINFO_EXTENSION))
        ))
    ;
'));
//echo to screen
header ('content-type: text/plain');
print_r ($dir);

有关代码中的更多详细信息,请单击此处