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 index, for example, peruse the name of the following section in the index.

Syntax:

readdir(dir_handle); 

Parameter(s):

dir_handle – it is a discretionary parameter which is utilized to determine the catalogue handle asset in the event that we don’t indicate the registry handle asset – it expects the last connection opened with opendir() function.

Return value:

In the event 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 a catalogue, 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!!