0

我有一个“路径/到/文件夹”。获取没有文件名的文件数组的最佳方法是什么?

我可以使用$files = scandir("path/to/folder/", 1); ,但我必须删除扩展名和“。” 从那个数组。有没有更好的方法来获取文件数组?

刚刚添加: PS我需要“路径/到/文件夹”中的文件,该文件夹中没有子文件夹。因此,如果该文件夹中的文件"ab.php, cd.php, 123.php, hello.php"

那么结果是:$files =array('ab', 'cd', '123', 'hello');

4

2 回答 2

3
$files = glob('path/to/files/*.*');
foreach($files as $file) {
    $file = pathinfo($file);
    echo $file['filename'];
}

或者,使用更快的DirectoryIterator类。

于 2012-12-31T20:23:30.350 回答
1

您可以尝试扩展FilesystemIterator您需要的所有内容:

echo new NoExtentionIterator(__DIR__);

使用的类

class NoExtentionIterator extends FilesystemIterator {
    public function current() {
        return pathinfo(parent::current(), PATHINFO_FILENAME);
    }
    public function accept() {
        return parent::current()->isfile();
    }
    public function __toString() {
        return implode("\n", iterator_to_array($this));
    }
}
于 2012-12-31T20:26:53.300 回答