PHP Tutorial - PHP dir() Function






Definition

The dir() function returns an instance of the Directory class.

Syntax

PHP dir() Function has the following syntax.

dir(directory,context);

Parameter

ParameterIs RequiredDescription
directoryRequired.Directory to open
contextOptional.Context to use

Return

PHP dir() Function returns an instance of the Directory class. FALSE on failure.





Example

Use the dir() function to read a directory:


<?php /*w w  w  . j a  va 2 s  .c  o m*/
$d = dir(getcwd());

echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";

while (($file = $d->read()) !== false){ 
  echo "filename: " . $file . "\n"; 
} 
$d->close(); 
?>

The code above generates the following result.