PHP readdir() Function

❮ PHP Directory Reference

Example

List all entries in the images directory, then close:

<?php
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>

Result:

filename: cat.gif
filename: dog.gif
filename: horse.gif


Definition and Usage

The readdir() function returns the name of the next entry in a directory.


Syntax

readdir(dir)

Parameter Values

Parameter Description
dir Optional. Specifies the directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is assumed


Technical Details

Return Value: The entry name (filename) on success, FALSE on failure
PHP Version: 4.0+

❮ PHP Directory Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.