php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73488 DirectoryIterator::isDir() strange behavior
Submitted: 2016-11-10 15:45 UTC Modified: 2016-11-10 16:53 UTC
From: stan dot wanderer at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: *Directory/Filesystem functions
PHP Version: 7.0.13 OS: ubuntu 16.04.1 lts x64
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 !
Your email address:
MUST BE VALID
Solve the problem:
15 + 19 = ?
Subscribe to this entry?

 
 [2016-11-10 15:45 UTC] stan dot wanderer at gmail dot com
Description:
------------
hello,

I've noticed that the DirectoryIterator object (as I know extended from SplFileInfo) has strange behavior of method isDir() - random boolean result on real directories.

I've tried to run my tests on different environments (the same results):
Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-45-generic x86_64)
# php -v
PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.8-0ubuntu0.16.04.3, Copyright (c) 1999-2016, by Zend Technologies

Ubuntu 14.04.5 LTS (GNU/Linux 4.8.3-x86_64-linode76 x86_64)
# php -v
PHP 5.5.9-1ubuntu4.19 (cli) (built: Jul 28 2016 19:31:33)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Maybe it is not a bug, but I cannot find any information about this issue.
Please, contact with me if you need any additional information.
Hope this will make PHP better!

Test script:
---------------
<?php

foreach (scandir(__DIR__) as $f)
    if (is_dir($f))
        echo sprintf("%20s: %d\n", $f, (new DirectoryIterator($f))->isDir());

Expected result:
----------------
                 bin: 1
                boot: 1
                 dev: 1
                 etc: 1
                home: 1
                 lib: 1
               lib64: 1
          lost+found: 1
               media: 1
                 mnt: 1
                 opt: 1
                proc: 1
                root: 1
                 run: 1
                sbin: 1
                 srv: 1
                 sys: 1
                 tmp: 1
                 usr: 1
                 var: 1

Actual result:
--------------
                 bin: 0
                boot: 1
                 dev: 1
                 etc: 0
                home: 1
                 lib: 1
               lib64: 0
          lost+found: 1
               media: 1
                 mnt: 1
                 opt: 1
                proc: 1
                root: 0
                 run: 1
                sbin: 1
                 srv: 1
                 sys: 1
                 tmp: 1
                 usr: 1
                 var: 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-10 16:06 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-11-10 16:06 UTC] requinix@php.net
As its name says, DirectoryIterator iterates over a directory. It does not represent the directory itself. It should thus come as no surprise that isDir will return whether the current item is a directory.

Try this:

foreach (scandir(__DIR__) as $f) {
    if (is_dir($f)) {
        $di = new DirectoryIterator($f);
        echo sprintf("%30s: %d\n", $di->getPathname(), $di->isDir());
    }
}
 [2016-11-10 16:17 UTC] stan dot wanderer at gmail dot com
-Status: Feedback +Status: Closed
 [2016-11-10 16:17 UTC] stan dot wanderer at gmail dot com
Very interesting :) Don't use Iterators as plain object - I understood... Thanks for your answer! I think this issue can be closed
 [2016-11-10 16:53 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2016-11-10 16:53 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Oct 03 05:05:53 2022 UTC