By Craig McKie
Hello,
I am trying to use RecursiveIteratorIterator with RecursiveDirectoryIterator to return an array of recursive files and folders.
This used to work on a Apache2 + php5 setup but I recently moved over to nginx and php5-fpm.
Everything on my site works perfectly except this particular function.
I do a var_dump on the variable those 2 functions are set on and it returns:
object(RecursiveIteratorIterator)#218 (0) { }
I dunno what’s wrong or if I’m missing a particular php5 package that’s suppose to be installed.
Anyone have any ideas?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi! RecursiveIteratorIterator is part of the base php5 package so it should be there by default. Are you getting any errors? Check nginx’s error log:
sudo tail /var/log/nginx/error.log
Heya,
For anyone else stumbling upon this in 2023, here’s a basic example of how you can use RecursiveIteratorIterator and RecursiveDirectoryIterator in PHP 8.2 to list files and directories recursively. This code should work in your environment, assuming there are no other underlying issues with your setup:
<?php
$directory = '/path/to/your/directory'; // Replace with your directory path
try {
$iterator = new RecursiveDirectoryIterator($directory);
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
$filesAndDirs = [];
foreach ($iterator as $file) {
if ($file->isDir()){
continue;
}
$filesAndDirs[] = $file->getPathname();
}
var_dump($filesAndDirs);
} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "\n";
}
?>
This script will list all files in the given directory and its subdirectories. If you encounter an empty object like object(RecursiveIteratorIterator)#218 (0) { }, it might be due to the following reasons:
Permissions Issue: Ensure that the user running your PHP-FPM process has read access to the directory you’re trying to list.
PHP Configuration: Check your php.ini file for any relevant differences that might affect iterator behavior.
Error Reporting: Make sure error reporting is enabled in your PHP configuration so that you can see any relevant warnings or errors. You can enable error reporting by adding error_reporting(E_ALL); ini_set('display_errors', 1); at the start of your script.
PHP Extensions: Certain PHP extensions might be required for this functionality. Ensure that all necessary extensions are installed and enabled in your PHP 8.2 setup.
Path Issue: Double-check the path you are providing to RecursiveDirectoryIterator. If the path is incorrect or inaccessible, it won’t be able to iterate over the directory.
Code Compatibility: Make sure that the rest of your code is compatible with PHP 8.2. PHP 8 introduced a number of breaking changes and type enforcement that might affect how your code behaves.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.