php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54256 DirectoryIterator isn't iterable with a foreach
Submitted: 2011-03-15 15:23 UTC Modified: 2013-02-18 00:34 UTC
Votes:8
Avg. Score:4.8 ± 0.4
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:1 (14.3%)
From: gege2061 at homecomputing dot fr Assigned:
Status: No Feedback Package: SPL related
PHP Version: 5.3.5 OS: Debian unstable
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: gege2061 at homecomputing dot fr
New email:
PHP Version: OS:

 

 [2011-03-15 15:23 UTC] gege2061 at homecomputing dot fr
Description:
------------
Hello,

DirectoryIterator is iterable in a while but not in a foreach.

This bug is reproductible only on a vboxsf filesystem. I run this script on Debian sid hosted on Windows XP (NTFS formated hd).

I have no problem when I execute this script on Windows or on a virtual disk.

Test script:
---------------
<?php
function test_iterator($dirname) {
    $files = array();

    $dir = new DirectoryIterator($dirname);
    while($dir->valid()) {
        $files[] = (string)$dir->current();
        $dir->next();
    }
    return $files;
}

function test_iterator_bug($dirname) {
    $files = array();

    $dir = new DirectoryIterator($dirname);
    foreach($dir as $file) {
        $files[] = (string)$file;
    }
    return $files;
}

echo 'PHP version: ' . phpversion() . "\n\n";
$dirname = dirname(__FILE__);
foreach (array('iterator', 'iterator_bug') as $test_name) {
    echo "--- {$test_name} ---\n";
    $foo = "test_{$test_name}";
    var_dump($foo($dirname));
    echo "\n";
}


Expected result:
----------------
PHP version: 5.3.5-1

--- iterator ---
array(3) {
  [0]=>
  string(1) "."
  [1]=>
  string(2) ".."
  [2]=>
  string(9) "index.php"
}

--- iterator_bug ---
array(3) {
  [0]=>
  string(1) "."
  [1]=>
  string(2) ".."
  [2]=>
  string(9) "index.php"
}


Actual result:
--------------
PHP version: 5.3.5-1

--- iterator ---
array(3) {
  [0]=>
  string(1) "."
  [1]=>
  string(2) ".."
  [2]=>
  string(9) "index.php"
}

--- iterator_bug ---
array(0) {
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-16 21:13 UTC] johannes@php.net
-Status: Open +Status: Feedback
 [2011-03-16 21:13 UTC] johannes@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/

Works perfectly for me. "PHP version: 5.3.5-1" is none of our version numbers. MAybe your distribution has patched this somehow. Please test our version or talk to the distributor.
 [2011-03-17 10:26 UTC] gege2061 at homecomputing dot fr
The problem is reproducible with php5.3-201103170730
 [2011-09-23 07:01 UTC] sniper dot unreal at web dot de
Hi Everyone,

I had the same problem on ubuntu (which is also debian based).
After installing the VirtualBox guest additions that are on the ISO that comes with VirtualBox instead of the guest additions bundled with debian/ubuntu solved the issue.

So you just have do install the guest additions on your VM and it should work fine.
 [2012-10-26 19:34 UTC] maurice dot k at zend dot com
The same problem occurs with Parallels Workstation under Windows 7, when using 
shared folders. DirectoryIterator is not iteratable using foreach.

Windows 7
Parallels Workstation v6
Linux Ubuntu 12.04.1-LTS

Any suggested workaround?
 [2013-02-18 00:34 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 "Open". Thank you.
 [2013-11-11 21:34 UTC] andergmartins at gmail dot com
I have the same issue, but using Parallels on a OS X.

I'm running a virtualized Debian 7, PHP 5.4.4-14+deb7u5 as my dev server.

The volume is mounted as a Parallels shared folder, in /media/psf/...

I have tested this script:
-----------------
<?php
$iterador = new RecursiveDirectoryIterator( __DIR__ . '/t1/');

foreach ($iterador as $file)
{
	var_dump($file);
}

-----------------
If the script is inside the shared folder, the result is a blank page.
If I execute the script in a not shared folder, a native linux path, it shows an iterator for each dir content.
 [2014-07-30 13:20 UTC] xpundel at gmail dot com
Debian under parallels on MacOS.
The same problem: DirectoryIterator return nothing on mounted shared filders

foreach (new DirectoryIterator('/media/psf/Web/') as $fileInfo)
{
	echo $fileInfo->getFilename() . "<br>\n";
}
 [2021-03-17 05:42 UTC] lne1030 at gmail dot com
I got the same problem.

Docker on Apline 3.13 within VirtualBox 5.2.44.

Sharing a folder from Windows host, mounting it in Alpine and binding it to a container.

Create a DirectoryIterator with its path in the container, traverse it by foreach get nothing, traverse it by while get expected data .

Upgrade VirtualBox to 6.1 and the problem was gone.
 [2021-03-17 05:44 UTC] lne1030 at gmail dot com
Forgot to mention the container is php:7.3-fpm-alpine
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC