Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Standard PHP Library (SPL) Functions : DirectoryIterator::__construct

DirectoryIterator::__construct

Constructs a new dir iterator from a path ()
DirectoryIterator DirectoryIterator::__construct ( string path )


Code Examples / Notes » directoryiterator_construct

pcdinh

The use of this class is rather simple
<?php
$dataDir  = dirname(__FILE__).'/world_vest_base/';

try
{
     $dir  = new DirectoryIterator($dataDir);
     foreach ($dir as $file)
     {
   $fileName = $file->getFilename();
     }
}
catch (Exception $ex)
{
}
?>
If $dataDir is null, a Runtime Exception will throws a message like this: Directory name must not be empty.
If $dataDir does not exist, that message will look like this:  failed to open dir: No such file or directory.
Please be careful in case you are looking for filenames in that directory because DirectoryIterator will return a very special symbolic filenames: . and .. You must do a check to ignore them before dealing with real file names.
$dir is a DirectoryIterator object


goran

I needed to match in directory tree file name(s) by regular expression. Code is based on Marcus Börger class DirectoryTreeIterator http://cvs.php.net/viewvc.cgi/php-src/ext/spl/examples/ and on examples given in his lecture Introduction to object oriented PHP at PHP Quebec conference 2007 http://talks.somabo.de/
<?php
class KeyFilter extends FilterIterator
{
 private $_rx;
 
 function __construct(Iterator $it, $regex)
 {
   parent::__construct($it);
   $this->_rx= $regex;
 }
 function accept()
 {
   return ereg($this->_rx,$this->getInnerIterator()->key());
 }
 protected function __clone() {
   return false;
 }
}
class DirMach extends KeyFilter
{
 function __construct($path , $regex)
 {
   parent::__construct(
   new DirectoryTreeIterator($path), $regex);
 }
 function current()
 {
   return parent::key();
 }
 function key()
 {
   return parent::key();
 }
}
class DirectoryTreeIterator extends RecursiveIteratorIterator
{
/** Construct from a path.
* @param $path directory to iterate
*/
function __construct($path)
{
try {
 parent::__construct(
new RecursiveCachingIterator(
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_FILENAME
),
CachingIterator::CALL_TOSTRING|CachingIterator::CATCH_GET_CHILD
),
parent::SELF_FIRST
);
} catch(Exception $e) {
  echo $e->getMessage();
  exit;
}
}
/** @return the current element prefixed with ASCII graphics
*/
function current()
{
 if ($this->hasChildren())
   $this->next();  
 return $this->key();
}
/** Aggregates the inner iterator
*/
function __call($func, $params)
{
 return call_user_func_array(array($this->getSubIterator(), $func), $params);
}
}
$PathToSearch = 'path_to_search';
$regex = 'regular_expression';
$FileList = new DirMach($PathToSearch, $regex);
foreach ($FileList as $file) {
 $match[] = $file;
}
echo '<pre>';
var_dump($match);
echo '</pre>';
?>


pcdinh

I need to traverse recursively through a directory tree and get all sub-directory paths and has written a snippet to do that.
<?php
$path = "D:\webroot\phpvietnamcms";
foreach (new RecursiveIteratorIterator(
          new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME), RecursiveIteratorIterator::CHILD_FIRST) as $file => $info) {
if ($info->isDir())
{
echo $file."<br />";
}
}
?>


Change Language


Follow Navioo On Twitter
ArrayIterator::current
ArrayIterator::key
ArrayIterator::next
ArrayIterator::rewind
ArrayIterator::seek
ArrayIterator::valid
ArrayObject::append
ArrayObject::__construct
ArrayObject::count
ArrayObject::getIterator
ArrayObject::offsetExists
ArrayObject::offsetGet
ArrayObject::offsetSet
ArrayObject::offsetUnset
CachingIterator::hasNext
CachingIterator::next
CachingIterator::rewind
CachingIterator::__toString
CachingIterator::valid
CachingRecursiveIterator::getChildren
CachingRecursiveIterator::hasChildren
DirectoryIterator::__construct
DirectoryIterator::current
DirectoryIterator::getATime
DirectoryIterator::getCTime
DirectoryIterator::getFilename
DirectoryIterator::getGroup
DirectoryIterator::getInode
DirectoryIterator::getMTime
DirectoryIterator::getOwner
DirectoryIterator::getPath
DirectoryIterator::getPathname
DirectoryIterator::getPerms
DirectoryIterator::getSize
DirectoryIterator::getType
DirectoryIterator::isDir
DirectoryIterator::isDot
DirectoryIterator::isExecutable
DirectoryIterator::isFile
DirectoryIterator::isLink
DirectoryIterator::isReadable
DirectoryIterator::isWritable
DirectoryIterator::key
DirectoryIterator::next
DirectoryIterator::rewind
DirectoryIterator::valid
FilterIterator::current
FilterIterator::getInnerIterator
FilterIterator::key
FilterIterator::next
FilterIterator::rewind
FilterIterator::valid
LimitIterator::getPosition
LimitIterator::next
LimitIterator::rewind
LimitIterator::seek
LimitIterator::valid
ParentIterator::getChildren
ParentIterator::hasChildren
ParentIterator::next
ParentIterator::rewind
RecursiveDirectoryIterator::getChildren
RecursiveDirectoryIterator::hasChildren
RecursiveDirectoryIterator::key
RecursiveDirectoryIterator::next
RecursiveDirectoryIterator::rewind
RecursiveIteratorIterator::current
RecursiveIteratorIterator::getDepth
RecursiveIteratorIterator::getSubIterator
RecursiveIteratorIterator::key
RecursiveIteratorIterator::next
RecursiveIteratorIterator::rewind
RecursiveIteratorIterator::valid
SimpleXMLIterator::current
SimpleXMLIterator::getChildren
SimpleXMLIterator::hasChildren
SimpleXMLIterator::key
SimpleXMLIterator::next
SimpleXMLIterator::rewind
SimpleXMLIterator::valid
class_implements
class_parents
iterator_count
iterator_to_array
spl_autoload_call
spl_autoload_extensions
spl_autoload_functions
spl_autoload_register
spl_autoload_unregister
spl_autoload
spl_classes
spl_object_hash
eXTReMe Tracker