PHP dir() Function

PHP getcwd() Function
PHP closedir() Function

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)
ParameterDescription
directoryRequired. Specifies the directory to open
contextOptional.
PHP dir() method

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();
?>
PHP getcwd() Function
PHP closedir() Function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top