php8.5
Home/ Manual/ spl / filesystemiterator/ FilesystemIterator::key

FilesystemIterator::key

PHP function Edit on GitHub ✎

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Retrieve the key for the current file

Description

FilesystemIterator::key(): string

Parameters Parameters

Return Values

Returns the pathname or filename depending on the set flags. See the FilesystemIterator constants.

Examples

FilesystemIterator::key example

This example will list the contents of the directory containing the script.

php
<?php
$iterator = new FilesystemIterator(dirname(__FILE__), FilesystemIterator::KEY_AS_FILENAME);
foreach ($iterator as $fileinfo) {
    echo $iterator->key() . "\n";
}
?>

Similar

output
.
..
apple.jpg
banana.jpg
example.php

See Also

Source: reference/spl/filesystemiterator/key.xml · from the official PHP manual (php/doc-en)