gpt4 book ai didi

PHP RecursiveDirectoryIterator 仅列出文件

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:39 25 4
gpt4 key购买 nike

我正在尝试用 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);

我该怎么做?谢谢

最佳答案

How to get only path to the file from RecursiveDirectoryIterator:

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

关于PHP RecursiveDirectoryIterator 仅列出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com