PHP opendir() Function

PHP readdir() Function
PHP getcwd() Function

In this article, you will learn how to open and read the directory in PHP. As the direct handle is used to implement the directory functionalities, so, the opendir() function in PHP opens a new directory handle.

what is the syntax of the OPENDIR() function in php?

opendir(path, context)
ParameterDescription
pathRequired. Specifies the directory path to be opened
contextOptional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream
PHP opendir() method

examples of the OPENDIR() function

Example 1. In this example, we open a directory, read its contents, 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);
  }
}
?>
PHP readdir() Function
PHP getcwd() Function
en English
X
Scroll to Top