Content · Programming · PHP

PHP FilesystemIterator::current() Method: Usage and Examples

Returns the current entry according to FilesystemIterator flags.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

FilesystemIterator::current() Method is one of the PHP operations used in filesystem and stream workflows. Returns the current entry according to FilesystemIterator flags. The examples below show what it does and how to handle its result.

What Is FilesystemIterator::current() Method?


Returns the current entry according to FilesystemIterator flags.

Its syntax is:

FilesystemIterator::current()

This function or method takes no parameters.

Return value: The declared return type is SplFileInfo|FilesystemIterator|string. With CURRENT_AS_FILEINFO it returns SplFileInfo, whereas CURRENT_AS_PATHNAME returns a string.

Important: With CURRENT_AS_FILEINFO it returns SplFileInfo, whereas CURRENT_AS_PATHNAME returns a string.

Example 1: Basic Usage


The basic example focuses on the behavior of FilesystemIterator::current() Method: returns the current entry according to FilesystemIterator flags. Inputs are defined locally or shown as application-controlled paths.

<?php

$entries = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
if ($entries->valid()) { var_dump($entries->current() instanceof SplFileInfo); }

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for FilesystemIterator::current() Method. Notice the different checks, arguments, or output handling.

<?php

$entries = new FilesystemIterator(__DIR__, FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS);
if ($entries->valid()) { echo $entries->current(); }

This variation matters because with CURRENT_AS_FILEINFO it returns SplFileInfo, whereas CURRENT_AS_PATHNAME returns a string.

Practical Example: Using the Operation in an Application


The following example demonstrates how FilesystemIterator::current() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO);
foreach ($iterator as $file) { if ($file->isFile()) { echo $file->getFilename() . "\n"; } }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Returns the current entry according to FilesystemIterator flags.
  • Limitation: With CURRENT_AS_FILEINFO it returns SplFileInfo, whereas CURRENT_AS_PATHNAME returns a string.

Comparison With Related Functions


Function or MethodDifference
FilesystemIterator::current() MethodReturns the current entry according to FilesystemIterator flags.
FilesystemIterator::key()Retrieves the key, while current() retrieves the current value according to active flags.

Use FilesystemIterator::current() Method for the specific behavior described above. The related operation, FilesystemIterator::key(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Confusing iterator position, keys and returned values.With CURRENT_AS_FILEINFO it returns SplFileInfo, whereas CURRENT_AS_PATHNAME returns a string. Verify cursor position and flags before reading or advancing entries.
Treating FilesystemIterator::key() as an interchangeable replacement for FilesystemIterator::current() Method.Retrieves the key, while current() retrieves the current value according to active flags.

Key Takeaways


  • Returns the current entry according to FilesystemIterator flags.
  • With CURRENT_AS_FILEINFO it returns SplFileInfo, whereas CURRENT_AS_PATHNAME returns a string.
  • Related operation: FilesystemIterator::key() — Retrieves the key, while current() retrieves the current value according to active flags.

FilesystemIterator::current() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.