I'm trying to make a code in PHP to list all files in directories and sub-directories in an array with their location, for this I'm using RecursiveDirectoryIterator
class. But when I print_r
my array, here's the result :
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
)
)
The result that i want:
Array(
images/test/foo.png
images/testing2/testt.png
images/testing3/flowers.png
)
My code:
$imagesList = array();
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('images/')) as $filename) {
if ($filename->isDir()) continue;
$imagesList[] = $filename;
}
print_r($imagesList);
How can I do this ? Thanks
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.