-1

I'm trying to use RecursiveIteratorIterator and RecursiveDirectoryIterator.

I want get all file inside my c:\ folder. But i don't know why i can't get the result but a blank page.

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('c:/' ));
foreach( $it as $file ) {
    $all[] = $file->getRealPath();
}
print_r($all);

but if i use this code, it's work

foreach( $it as $key=>$file )
{
    echo $key."=>".$file."\n";
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Ahmad
  • 4,224
  • 8
  • 29
  • 40

2 Answers2

3

Most likely because your PHP interpreter does not have access to that folder.

Devraj
  • 3,025
  • 24
  • 25
2

It's because PHPs StdObj-class cannot be used as an array because it's an associative array. This is wrong by the words but I cannot describe it better. The PHP object gets casted or something like that.

If $data is an object then this is required if you want to access $value as an object.

foreach($data as $property => $value){
    echo $value->r;
}

Edit: This is a good question btw, I've spent a couple of hours myself figuring this out.

Gustav
  • 2,902
  • 1
  • 25
  • 31