readdir

(PHP 4, PHP 5, PHP 7, PHP 8)

readdirRead entry from directory handle

Description

readdir(?resource $dir_handle = null): string|false

Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.

Parameters

dir_handle

The directory handle resource previously opened with opendir(). If the directory handle is not specified, the last link opened by opendir() is assumed.

Return Values

Returns the entry name on success or false on failure.

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Changelog

Version Description
8.0.0 dir_handle is now nullable.

Examples

Example #1 List all entries in a directory

Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) false since otherwise, any directory entry whose name evaluates to false will stop the loop (e.g. a directory named "0").

<?php

if ($handle opendir('/path/to/files')) {
    echo 
"Directory handle: $handle\n";
    echo 
"Entries:\n";

    
/* This is the correct way to loop over the directory. */
    
while (false !== ($entry readdir($handle))) {
        echo 
"$entry\n";
    }

    
/* This is the WRONG way to loop over the directory. */
    
while ($entry readdir($handle)) {
        echo 
"$entry\n";
    }

    
closedir($handle);
}
?>

Example #2 List all entries in the current directory and strip out . and ..

<?php
if ($handle opendir('.')) {
    while (
false !== ($entry readdir($handle))) {
        if (
$entry != "." && $entry != "..") {
            echo 
"$entry\n";
        }
    }
    
closedir($handle);
}
?>

See Also

  • is_dir() - Tells whether the filename is a directory
  • glob() - Find pathnames matching a pattern
  • opendir() - Open directory handle
  • scandir() - List files and directories inside the specified path

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Category: Software & Web-Development

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial