PHP dir() function: Here, we will find out about the PHP dir() function with its syntax, parameters, returns a type, and example.
PHP dir() function
dir() function is an occurrence of the index class, it is utilized to peruse the catalogue, it incorporates handle and way properties – which can be utilized to get the asset id and way to the registry. Both handle and way have perused(), rewind(), and close() strategies.
Syntax:
dir(directory,context);
Parameter(s):
- index – It is utilized to determine the way to the catalogue to be opened.
- setting – It is a discretionary parameter; it characterizes the specific situation (a lot of alternatives that can change the conduct of the stream).
Return value:
On progress – it restores an occurrence to the catalogue, on fizzle – it returns “FALSE“.
Example: PHP code to show example of dir() function
<?php
//path to current working directory
$cwd = dir(getcwd());
echo "Directory handle: " . $cwd->handle . "<br>";
echo "Current path: " . $cwd->path . "<br>";
//reading & printing the filenames using dir()
while (($file = $cwd->read()) !== false){
echo "File: " . $file . "<br>";
}
//closing the dir instance
$cwd->close();
?>
Output
Directory handle: Resource id #5
Current path: /home
File: .
File: ..
File: main.php
Reference: PHP dir() function