How can I retrieve the full directory tree using SPL, possibly using RecursiveDirectoryIterator and RecursiveIteratorIterator?
How can I retrieve the full directory tree using SPL , possibly using RecursiveDirectoryIterator
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By default, the
RecursiveIteratorIteratorwill useLEAVES_ONLYfor the second argument to__construct. This means it will return files only. If you want to include files and directories (at least that’s what I’d consider a full directory tree), you’d have to do:and then you can
foreachover it. If you want to return the directory tree instead of outputting it, you can store it in an array, e.g.You can also create the array of
$fileObjectswithout theforeachby doing:If you only want directories returned,
foreachover the$iteratorlike this: