php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64533 recursive iterator makes dir become file
Submitted: 2013-03-27 13:37 UTC Modified: 2013-10-15 11:54 UTC
From: kasper at webmasteren dot eu Assigned:
Status: No Feedback Package: SPL related
PHP Version: Irrelevant OS: Windows 8 x64
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-03-27 13:37 UTC] kasper at webmasteren dot eu
Description:
------------
It is possible for the following code , to change a FOLDER to a "file", by SPL's 
functions, which only from what i have seen occures by the recursive itterator.

all there is needed is a folder structure like:
pages/a
       /main
       /main.top
       /main.content
       /main.menu

and those folders all have at least a file, in my particular case, "view.php".
The example, even prints what the old standard functions tell about the "path" 
from the element, and what the spl provides. 

Test script:
---------------
$recursiveIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator("pages\\a", \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($recursiveIterator as $element) { //the child first => top lvl first.
    var_dump("path:" . $element->getPath());

    if ($element->isFile()) {
        var_dump("Is file by new function(SPL)");
    }
    if($element->isDir()){
        var_dump("is dir by new function");
    }

    if (\is_dir($element->getPath())) {
        var_dump("Is dir by old function");
    }
    
    if(\is_file($element->getPath())){
        var_dump("is file by old function");
    }
}
$f = new \SplFileInfo("pages\\a\\main");
var_dump($f->isDir()); //SHOULD BE TRUE!!

Expected result:
----------------
that SPL and the old functions would agree, and folders were reportet as folders.

Actual result:
--------------
That in SPL , inside of Recursive iterator and possibly recursiveDirectoryIterator 
folders becomes seen as files, causing it to report wrong type (only spl, not the 
old functions).


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-27 14:26 UTC] pajoye@php.net
Is it only:

$f = new \SplFileInfo("pages\\a\\main");
var_dump($f->isDir()); //SHOULD BE TRUE!

that fails? Or previous calls fail too? Inside the foreach.

Also if you could provide a script that creates the structure and reproduce the 
bug, that would make the debugging session easier.
 [2013-03-27 14:26 UTC] pajoye@php.net
-Status: Open +Status: Feedback
 [2013-03-27 16:49 UTC] kasper at webmasteren dot eu
its is all inside of the loop that is wrong, it will print that by SPL the 
"folders" are files, (isFile() is true, and isDir() is false)
so heres a snippet to make the test files'n folders:
-----------------------------------
mkdir("pages");
mkdir("pages/a");
mkdir("pages/a/main");
mkdir("pages/a/main.menu");
mkdir("pages/a/main.bottom");
mkdir("pages/a/main.top");
fopen("pages/a/main/view.php","w+");
fopen("pages/a/main.menu/view.php","w+");
fopen("pages/a/main.bottom/view.php","w+");
fopen("pages/a/main.top/view.php","w+");
-----------------------------------
and then just use the orginal snippet.
what it will print is: 
string 'path:pages/a\main' (length=17)
string 'Is file by new function(SPL)' (length=28)
string 'Is dir by old function' (length=22)

which clealy shows that something must be wrong. 

the last line, the
var_dump($f->isDir()); //SHOULD BE TRUE!!
is to insure that the SplFileInfo is actually working, which means it must be in 
the iterator part ,somehow stuff gets wrong. (isDir()=true, so )
 [2013-10-15 11:54 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC