PHP dir() function with example

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 example of the catalogue class, it is utilized to peruse the registry, it incorporates handle and way properties – which can be utilized to get the asset id and way to the index. Both handle and way have perused(), rewind(), and close() techniques.

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 unique situation (a lot of alternatives that can change the conduct of the stream).

Return value:

On progress – it restores a case to the index, on coming up short – 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

ReferencePHP dir() function

Leave a Comment

error: Alert: Content is protected!!