PHP opendir() function with example

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

PHP opendir() function

The full type of opendir is “Open Directory“, the function opendir() is utilized to open an index.

Syntax:

opendir(path,context); 

Parameter(s):

  • way – It is the way (name) to the index to be opened.
  • setting – It is a discretionary parameter; it characterizes the specific situation (a lot of the alternatives that can adjust the conduct of the flood) of the catalogue handle.

Return value:

On the off chance that function execution is an achievement – it restores the catalogue handle asset if function execution is fizzled – it restores the “FALSE“.

Example: PHP code to open an index, read its documents, 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 = opendir($path)){
    while (($file = readdir($dh)) !== false){
      echo "File:" . $file . "<br/>";
    }
    //closing the directory
    closedir($dh);
  }
}
?>

Output

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

Reference: PHP opendir() function

Leave a Comment

error: Alert: Content is protected!!