DirectoryIterator::__construct
(PHP 5)
DirectoryIterator::__construct — Constructs a new directory iterator from a path
Description
public DirectoryIterator::__construct
( string
$path
)Constructs a new directory iterator from a path.
Parameters
-
path
-
The path of the directory to traverse.
Errors/Exceptions
Throws an UnexpectedValueException
if the path
cannot be opened.
Throws a RuntimeException
if the path
is an empty string.
Changelog
Version | Description |
---|---|
5.3.0 |
Throws UnexpectedValueException if the
path cannot be opened.
|
5.1.3 |
Throws RuntimeException if the
path is an empty string.
|
5.1.0 | Throws RuntimeException on error. Previously, threw Exception. |
Examples
Example #1 A DirectoryIterator::__construct() example
This example will list the contents of the directory containing the script.
<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
var_dump($fileinfo->getFilename());
}
}
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Стандартная библиотека PHP (SPL)
- Итераторы
- Функция DirectoryIterator::__construct() - Создаёт новый итератор директорий по пути
- Функция DirectoryIterator::current() - Возвращает текущий элемент DirectoryIterator
- Функция DirectoryIterator::getATime() - Возвращает время последнего доступа к текущему элементу DirectoryIterator
- Функция DirectoryIterator::getBasename() - Возвращает имя файла (без расширения) текущего элемента DirectoryIterator
- Функция DirectoryIterator::getCTime() - Возвращает время последнего изменения i-узла текущего элемента DirectoryIterator
- Функция DirectoryIterator::getExtension() - Возвращает расширение файла
- Функция DirectoryIterator::getFilename() - Возвращает имя файла текущего элемента DirectoryIterator
- Функция DirectoryIterator::getGroup() - Возвращает идентификатор группы текущего элемента DirectoryIterator
- Функция DirectoryIterator::getInode() - Возвращает inode текущего элемента DirectoryIterator
- Функция DirectoryIterator::getMTime() - Возвращает время последнего изменения текущего элемента DirectoryIterator
- Функция DirectoryIterator::getOwner() - Возвращает индентификатор владельца текущего элемента DirectoryIterator
- Функция DirectoryIterator::getPath() - Возвращает путь к текущему элементу DirectoryIterator без имени файла
- Функция DirectoryIterator::getPathname() - Возвращает путь и имя файла текущего элемента DirectoryIterator
- Функция DirectoryIterator::getPerms() - Возвращает набор прав для текущего элемента DirectoryIterator item
- Функция DirectoryIterator::getSize() - Возвращает размер текущего элемента DirectoryIterator
- Функция DirectoryIterator::getType() - Определяет тип текущего элемента DirectoryIterator
- Функция DirectoryIterator::isDir() - Определяет, является ли текущий элемент DirectoryIterator директорией
- Функция DirectoryIterator::isDot() - Определяет, является ли текущий элемент DirectoryIterator '.' или '..'
- Функция DirectoryIterator::isExecutable() - Определяет, является ли текущий элемент DirectoryIterator исполняемым
- Функция DirectoryIterator::isFile() - Определяет, является ли текущий элемент DirectoryIterator обычным файлом
- Функция DirectoryIterator::isLink() - Определяет, является ли текущий элемент DirectoryIterator символической ссылкой
- Функция DirectoryIterator::isReadable() - Определяет, доступен ли текущий элемент DirectoryIterator для чтения
- Функция DirectoryIterator::isWritable() - Определяет, доступен ли текущий элемент DirectoryIterator для записи
- Функция DirectoryIterator::key() - Возвращает ключ текущего элемента DirectoryIterator
- Функция DirectoryIterator::next() - Перемещает указатель на следующий элемент DirectoryIterator
- Функция DirectoryIterator::rewind() - Устанавливает указатель на первый элемент DirectoryIterator
- Функция DirectoryIterator::seek() - Перемещает указатель DirectoryIterator на определённую позицию
- Функция DirectoryIterator::__toString() - Возвращает имя файла в виде строки
- Функция DirectoryIterator::valid() - Проверяет, является ли текущий элемент DirectoryIterator допустимым файлом
Коментарии
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();
}
?>
udvig dot ericson at gmail dot com
29-Jul-2006 07:48
In response to the comment below from udvig. The Example ist wrong, here is the right one:
<?php
$dir = new DirectoryIterator("/tmp");
foreach ($dir as $file) {
if ($file->isDot()) {
continue;
}
echo $file->getFilename() . "\n";
}
?>
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>';
?>
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 />";
}
}
?>
Don't store DirectoryIterator objects for later; you will get an error saying "too many open files" when you store more than the operating system limit (usually 256 or 1024).
For example, this will yield an error if the directory has too many files:
<?php
$files = array();
foreach (new DirectoryIterator('myDir') as $file) {
$files[] = $file;
}
?>
Presumably, this approach is memory intensive as well.
Here's all-in-one DirectoryIterator:
<?php
/**
* Real Recursive Directory Iterator
*/
class RRDI extends RecursiveIteratorIterator {
/**
* Creates Real Recursive Directory Iterator
* @param string $path
* @param int $flags
* @return DirectoryIterator
*/
public function __construct($path, $flags = 0) {
parent::__construct(new RecursiveDirectoryIterator($path, $flags));
}
}
/**
* Real RecursiveDirectoryIterator Filtered Class
* Returns only those items which filenames match given regex
*/
class AdvancedDirectoryIterator extends FilterIterator {
/**
* Regex storage
* @var string
*/
private $regex;
/**
* Creates new AdvancedDirectoryIterator
* @param string $path, prefix with '-R ' for recursive, postfix with /[wildcards] for matching
* @param int $flags
* @return DirectoryIterator
*/
public function __construct($path, $flags = 0) {
if (strpos($path, '-R ') === 0) { $recursive = true; $path = substr($path, 3); }
if (preg_match('~/?([^/]*\*[^/]*)$~', $path, $matches)) { // matched wildcards in filename
$path = substr($path, 0, -strlen($matches[1]) - 1); // strip wildcards part from path
$this->regex = '~^' . str_replace('*', '.*', str_replace('.', '\.', $matches[1])) . '$~'; // convert wildcards to regex
if (!$path) $path = '.'; // if no path given, we assume CWD
}
parent::__construct($recursive ? new RRDI($path, $flags) : new DirectoryIterator($path));
}
/**
* Checks for regex in current filename, or matches all if no regex specified
* @return bool
*/
public function accept() { // FilterIterator method
return $this->regex === null ? true : preg_match($this->regex, $this->getInnerIterator()->getFilename());
}
}
?>
Some examples:
<?php
/* @var $i DirectoryIterator */
foreach (new AdvancedDirectoryIterator('.') as $i) echo $i->getPathname() . '<br/>';
// will output all files and directories in CWD
foreach (new AdvancedDirectoryIterator('-R *.php') as $i) echo $i->getPathname() . '<br/>';
// will output all php files in CWD and all subdirectories
foreach (new AdvancedDirectoryIterator('-R js/jquery-*.js') as $i) echo $i->getPathname() . '<br/>';
// will output all jQuery versions in directory js, or throw an exception if directory js doesn't exist
?>
Pretty cool, huh? :)
Beware that DirectoryIterator returns a reference to itself with each iteration instead of a unique new SplFileInfo object as one may first assume.
This is important to know if you want to push the results of an iteration into an array for later processing.
If you do that, then all entries in the array will be the same DirectoryIterator object with the same internal state (most likely invalid - see the valid() method), which is probably not what you want.
So if you do want to store the iteration results in an array, then you must convert them to either plain strings or new SplFileInfo objects first.
$todo = [];
foreach (new DirectoryIterator('/some/dir') as $entry) {
if (!$entry->isFile()) {
continue;
}
#$todo []= $entry; # Don't do this!
$todo []= new SplFileInfo($entry->getFilename()); # Do this!
#$todo []= $entry->getFilename(); # or do this if you're only interested in the file name.
}
Many developers get confused between DirectoryIterator and FilesystemIterator, there are a few small differences that I'm goning to list them here:
DirectoryIterator:
* Includes the "." and ".." within the path.
* Numberd keys.
* Path is not included in in value.
* No configuration available.
* You do need to use "clone" when storing the elements into an array.
Example:
<?php
$files = [];
foreach ($dir as $key => $item) {
echo $key.PHP_EOL; // The key is a number
$files[] = $item;
}
echo $files[0]->getFilename(); // nothing happens here, you do need to use clone $item inside the foreach
?>
FilesystemIterator:
* Dot files skipped.
* Pathname is used as a key as well as a value.
* Configurable.
* Uses SplFileInfo to store the files.
"FilesystemIterator" it's an enhanced version of DirectoryIterator, and I prefer to use it instead of the DirectoryIterator.
RecursiveDirectoryIterator seems to close file handles that are currently open. After using it "PHP Warning: fwrite(): supplied resource is not a valid stream resource in..." is given...