回应文件夹中的文件

时间:2014-12-08 04:23:52

标签: php arrays directory echo

我想创建一个MP3或音频页面,并想知道我如何为从用户上传到该文件夹​​的文件制作一个数组,然后在页面上作为USABLE MP3或其他音频文件回显,上次上传的较新文件需要位于页面顶部。我有一个上传页面工作,将文件带到一个文件夹,但就是这样。

2 个答案:

答案 0 :(得分:0)

您好您可以使用名为scandir()的内置php函数,当您在参数中传递文件夹路径时,它将返回文件数组。

http://php.net/manual/en/function.scandir.php

示例:

<?php
$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r($files1);
print_r($files2);
?>

输出:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

答案 1 :(得分:0)

我认为它类似......

$dir    = '/mp3s';
$files = scandir($dir);
foreach($files as $file) {
    if(preg_match('~\.(mp3|EXTEnSION)$~'. $file)) {
        echo '<a href="' . $dir . '/' . $file . '">'  . $file , '</a>';
    }
}

然后您需要修改scandir函数。这个话题似乎涵盖了这一点。

scandir() to sort by date modified

可以通过添加您想要通过管道分隔的任何其他扩展来修改正则表达式。区分大小写目前附加了一个&#39; i&#39;如果你不想要它,那么在波浪之后。