php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75188 RecursiveIteratorIterator::SELF_FIRST not working
Submitted: 2017-09-11 13:07 UTC Modified: -
From: candeliere at trendengineering dot it Assigned:
Status: Open Package: SPL_Types (PECL)
PHP Version: 7.1.9 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
35 - 8 = ?
Subscribe to this entry?

 
 [2017-09-11 13:07 UTC] candeliere at trendengineering dot it
Description:
------------
According to the (hard to find) documentation, SELF_FIRST should output "branches" first, then "leaves" (in case of RecursiveDirectoryIterator, it should output folders first, then files) but it is not doing that. Apparently it returns the folder structure as the underlying filesystem is returning it untouched.
NOTE: Please understand that I'm not talking about *alphabetical* order. This is related to the fact that the test script below will generate an error ("no such file or directory)
Actually the first iteration will try to copy /<my/custom/path>/themes/SimpleSoloEmailVideo/content.php, but /<my/custom/path>/themes/SimpleSoloEmailVideo/ doesn't exist yet.
The test code would work perfectly if the SELF_FIRST order is guaranteed.
 

Test script:
---------------
    function copy_recursive($source, $dest)
    {
        if (is_dir($source)) {
            $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
                RecursiveIteratorIterator::SELF_FIRST
            );
            foreach ($iterator as $file) {
                if ($file->isDir()) {
                    mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName(), 0777, true);
                } else {
                    copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
                }
                error_log($file); 
            }
        } else {
            copy($source, $dest);
        }
    }


Expected result:
----------------
/<my/custom/path>/themes/SimpleSoloEmailVideo/css/
/<my/custom/path>/themes/SimpleSoloEmailVideo/css/style.css
/<my/custom/path>/themes/SimpleSoloEmailVideo/js/
/<my/custom/path>/themes/SimpleSoloEmailVideo/js/mycoolscript.js
/<my/custom/path>/themes/SimpleSoloEmailVideo/_imgs/
/<my/custom/path>/themes/SimpleSoloEmailVideo/content.php
/<my/custom/path>/themes/SimpleSoloEmailVideo/snapshot.png
/<my/custom/path>/themes/SimpleSoloEmailVideo/_hotarea_sample.php
/<my/custom/path>/themes/SimpleSoloEmailVideo/theme.xml
/<my/custom/path>/themes/SimpleSoloEmailVideo/thumb-65x65.png


Actual result:
--------------
/<my/custom/path>/themes/SimpleSoloEmailVideo/content.php
/<my/custom/path>/themes/SimpleSoloEmailVideo/snapshot.png
/<my/custom/path>/themes/SimpleSoloEmailVideo/css
/<my/custom/path>/themes/SimpleSoloEmailVideo/css/style.css
/<my/custom/path>/themes/SimpleSoloEmailVideo/js
/<my/custom/path>/themes/SimpleSoloEmailVideo/js/mycoolscript.js
/<my/custom/path>/themes/SimpleSoloEmailVideo/_hotarea_sample.php
/<my/custom/path>/themes/SimpleSoloEmailVideo/theme.xml
/<my/custom/path>/themes/SimpleSoloEmailVideo/_imgs
/<my/custom/path>/themes/SimpleSoloEmailVideo/thumb-65x65.png


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Tue Jul 17 11:01:54 2018 UTC