PHP readdir() function with example

PHP readdir() function with example

PHP readdir() function: Here, we will find out about the PHP readdir() function with its syntax, parameters, returns type, and example.

PHP readdir() function

The full type of readdir is “Read Directory“, the function readdir() is utilized to peruse the catalogue, for example, peruse the name of the following section in the registry.

Syntax:

readdir(dir_handle); 

Parameter(s):

  • dir_handle – it is a discretionary parameter which is utilized to indicate the index handle asset on the off chance that we don’t determine the registry handle asset – it accepts the last connection opened with opendir() function.

Return value:

On the off chance that the function execution is an achievement – it restores the filename if the function execution is fizzled – it returns “False”.

Example: PHP code to open an index, read its records, and close it

<?php

$path = "/home";

//checking whether $path is a directory or not
//then, opening the directory and reading its files
if (is_dir($path)){
  if ($dh = readdir($path)){
    while (($file = readdir($dh)) !== false){
      echo "File:" . $file . "<br/>";
    }
    //closing the directory
    readdir($dh);
  }
}
?>

Output

File:..
File:main.php
File:.

Reference: PHP readdir() function

Recent Posts

error: Alert: Content is protected!!