PHP RecursiveDirectoryIterator仅列出文件

时间:2016-01-09 13:39:43

标签: php arrays

我试图在PHP中创建一个代码来列出目录中的所有文件以及它们所在位置的数组中的子目录,为此我使用RecursiveDirectoryIterator类。但是当我print_r我的数组时,这里的结果

Array
(
    [0] => SplFileInfo Object
        (
            [pathName:SplFileInfo:private] => images\test\foo.png
            [fileName:SplFileInfo:private] => foo.png
        )

    [1] => SplFileInfo Object
        (
            [pathName:SplFileInfo:private] => images\testing2\testt.png
            [fileName:SplFileInfo:private] => testt.png
        )

    [2] => SplFileInfo Object
        (
            [pathName:SplFileInfo:private] => images\testing3\flowers.png
            [fileName:SplFileInfo:private] => flowers.png
        )
)

我想要的结果:

Array(
   images/test/foo.png
   images/testing2/testt.png
   images/testing3/flowers.png
)

我的代码:

    $imagesList = array();
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('images/')) as $filename) {
        if ($filename->isDir()) continue;
        $imagesList[] = $filename;
    }
    print_r($imagesList);

我该怎么做?感谢

1 个答案:

答案 0 :(得分:2)

  

如何从RecursiveDirectoryIterator获取文件的路径:

$imagesList = array();
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('images/')) as $filename) {
    if ($filename->isDir()) continue;
    $imagesList[] = str_replace("\\","/",$filename->getPathname());
}
print_r($imagesList);