fork download
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. $dir = '/tmp/test';
  7. @mkdir($dir, 0777, true);
  8. touch($dir . '/.anyhidden'); // like .DS_Store on mac...
  9. @mkdir($dir . '/mydir');
  10.  
  11.  
  12. class DirFilterIterator extends \FilterIterator
  13. {
  14. public function accept()
  15. {
  16. if ($this->current()->isDir()) {
  17. return true;
  18. }
  19. return false;
  20. }
  21. }
  22.  
  23.  
  24. $flags = \FilesystemIterator::KEY_AS_PATHNAME |
  25. \FilesystemIterator::CURRENT_AS_FILEINFO;
  26.  
  27. //$flags |= \FilesystemIterator::SKIP_DOTS; // uncomment this line to see the bug?
  28.  
  29.  
  30. $it = new \RecursiveDirectoryIterator($dir, $flags);
  31.  
  32. $rit = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST, 0);
  33. $rit = new DirFilterIterator($rit);
  34. $iterator = new \AppendIterator();
  35. $iterator->append($rit);
  36.  
  37. foreach ($iterator as $f) {
  38. /**
  39.   * @var \SplFileInfo $f
  40.   */
  41. if ('.' !== $f->getBasename() && '..' !== $f->getBasename()) {
  42. echo sprintf('pathname: %s, filename: %s', $f->getPathname(), $f->getFilename());
  43. echo '<br />';
  44. }
  45. }
  46.  
Runtime error #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Warning:  touch(): Unable to create file /tmp/test/.anyhidden because No such file or directory in /home/Wwaiqf/prog.php on line 8
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/tmp/test): failed to open dir: No such file or directory' in /home/Wwaiqf/prog.php:30
Stack trace:
#0 /home/Wwaiqf/prog.php(30): RecursiveDirectoryIterator->__construct('/tmp/test', 0)
#1 {main}
  thrown in /home/Wwaiqf/prog.php on line 30