Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

dir()

Example

Utilize 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();
?>

Result:

Handle: Resource id #2
Path: /etc/php
filename: .
filename: ..
filename: ajax.gif
filename: books.xml
filename: cdcatalog.xml
filename: cd_catalog.xml
filename: default.asp
filename: demo_array.asp
filename: demo_array.htm


Definition and Usage

The dir() function returns an instance of the Directory class. This function is used to interact with a directory, providing the following features:

  • The specified directory is opened.
  • The handle and path properties of the dir() object are accessible.
  • Both handle and path properties include three methods: read(), rewind(), and close().

Syntax

dir(directorycontext)

Parameter Values

Parameter

Description

directory

Required. Specifies the directory to be opened.

context

Optional.

Technical Details

 

Return Value:

Returns an instance of the Directory class or FALSE on failure.

PHP Version:

4.0+