Report this

What is the reason for this report?

RecursiveIteratorIterator does not work - php5-fpm

Posted on April 27, 2015

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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:

  1. Permissions Issue: Ensure that the user running your PHP-FPM process has read access to the directory you’re trying to list.

  2. PHP Configuration: Check your php.ini file for any relevant differences that might affect iterator behavior.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.