从文件夹中获取文件并按类型对其进行排序

时间:2014-10-21 07:19:15

标签: php

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

文件(文件夹)     .PDF     .PDF     .DOC     .DOC     .DOC     。压缩     .ZIP
的index.php

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

对于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>

对于doc:

<div class="dopobrania">
    <a href="file adress">file title</a>
</div>
你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您应该尝试使用此代码,但您可以找到出路。

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

有关代码的详细信息,请点击here