DirectoryIterator::__construct

(no version information, might be only in CVS)

DirectoryIterator::__construct --  Constructs a new dir iterator from a path

Description

DirectoryIterator DirectoryIterator::__construct ( string path )

警告

本函数暂无文档,仅有参数列表。


add a note add a note User Contributed Notes
ludvig dot ericson at gmail dot com
29-Jul-2006 01:48
In response to the comment below, you don't have to simulate a foreach(), DirectoryIterator obviously inherits SPL's Iterator interface, therefore:

<?php
$dir
= new DirectoryIterator("/tmp");
foreach (
$dir as $file) {
   if (
$dir->isDot()) {
       continue;
   }
   echo
$file . "\n";
}
?>
fernandobassani at gmail dot com
26-Jan-2006 02:36
We can now replace the old fashioned way of listing the content from a directory!

the old way:
<?php
if ($handle = opendir('/home/fernando/temp')) {
   while (
false !== ($file = readdir($handle))) {
       if (
$file != "." && $file != "..") {
           print
"$file <br />";
       }
   }
  
closedir($handle);
}
?>

the new way:
<?php
$dir
= new DirectoryIterator('/home/fernando/temp');
while(
$dir->valid()) {
   if(!
$dir->isDot()) {
       print
$dir->current()."<br />";
   }
  
$dir->next();
}
?>
jakob dot buchgraber at gmail dot com
04-Jan-2006 11:36
I wrote a function for finding all files in the current and in subdirectories.

The Code:
<?php
function getFiles(&$rdi,$depth=0) {

   if (!
is_object($rdi))
       return;
      
   for (
$rdi->rewind();$rdi->valid();$rdi->next()) {
      
       if (
$rdi->isDot())
           continue;
      
       if (
$rdi->isDir() || $rdi->isFile()) {
          
           for (
$i = 0; $i<=$depth;++$i)
               echo
'&nbsp;&nbsp;&nbsp;';
              
           echo
$rdi->current().'<br />';
          
           if (
$rdi->hasChildren())
              
getFiles($rdi->getChildren(),1+$depth);
       }
   }
}

getFiles(new RecursiveDirectoryIterator('.'));
?>