|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 11:00:01 2025 UTC |
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()); } }