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)
Parameter | Description |
---|---|
path | Required. Specifies the directory path to be opened |
context | Optional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream |
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);
}
}
?>