In this article, you will learn how to read a directory in PHP. The dir() function in PHP returns an instance of the Directory class. This function is used to read a directory that is opened.
what is the syntax of the DIR() function in php?
dir(directory, context)
Parameter | Description |
---|---|
directory | Required. Specifies the directory to open |
context | Optional. |
examples of the DIR() function
Example 1. In this example, we use the dir() function.
<?php
$d = dir(getcwd());
echo "Handle: " . $d->handle . "<br>";
echo "Path: " . $d->path . "<br>";
while (($file = $d->read()) !== false){
echo "filename: " . $file . "<br>";
}
$d->close();
?>