Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Directory dir() Function



The PHP Directory dir() Function is used to create an object of the directory class. It generates a directory class handle that can be used to read and rewind the directory's contents (files and folders). In addition to this we can also close the directory handle.

The dir() function opens a directory handle and returns an object. The object contains three methods namely −
  • read(): To get a list of every file and folder in the directory-not the subfolders-use the read() function.
  • rewind(): To begin the read() process again, use this function first.
  • close(): To stop using the directory handle, use the close() function.

Syntax

Following is the syntax of the PHP Directory dir() Function −

dir(string $directory, ?resource $context = null): Directory|false

Parameters

Here are the required and optional parameters of the dir() function −

Sr.No Parameter & Description
1

$directory(Required)

The directory path to be opened.

2

$context (Optional):

A context stream resource. Default value is null.

Return Value

The directory function dir() returns directory handle on success or FALSE on failure.

PHP Version

The dir() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.

Example

Following is the basic example of the PHP Directory dir() function −

<php
   // Open the directory
   $directory = dir("/PhpProjects"); 

   // Read entries
   while (($entry = $directory->read()) !== false) { 
      // Print each entry
      echo $entry . "<br>"; 
   }

   // Close the directory
   $directory->close(); 
?>

Output

This will generate the following output −

.
..
index.php
README.md
images
css
js

Example

In this PHP code example we will list the contents of a directory (/var/www in our case) using dir() function and show the information about each entry inside that directory. It reads each entry from the directory using a while loop. The loop runs until there are no more entries to read. After reading all the entries the directory handle is closed with the close() method.

<?php
   $d = dir("/var/www/");
   echo "Handle: " . $d->handle . "\n";
   echo "Path: " . $d->path . "\n";
   
   while (false !== ($entry = $d->read())) {
      echo $entry."\n";
   }
   $d->close();
?> 

This will produce the following result −

Handle: Resource id #5
Path: /var/www
..
.
html

Example

In this example we will show how we can use the dir() function in PHP to list the contents of a directory. So we will just open a directory called "new dir" which should be located in the current directory. It then lists all the contents of this directory (files and folders) and shows each entity's name as the outcome.

<?php
   // open the directory
   $directory_handle = dir("./new dir");

   // check if the directory handle is valid or not
   if ($directory_handle) {
      echo "Contents of the directory:<br>";

      // Loop over every entry in the directory
      while (($entry = $directory_handle->read()) !== false) {
         echo $entry . "<br>"; // Output each entry
      }
    
      // close the directory handle using close()
      $directory_handle->close();
   } else {
      echo "Failed to open directory.";
   }
?> 

Output

This will produce the below outcome −

Contents of the directory:
.
..
Pictures
my_folder
index.txt
PHP
my.txt

Example

In the below PHP code we will use all the three methods of dir class. So basically we will list the contents available in the given directory and rewind it using rewind() function and then close it using the close() function.

<?php
   $directory_handle = dir("./images/");
   echo "Directory Handle: " . $directory_handle->handle . "<br>";
   echo "Directory Path: " . $directory_handle->path . "<br>";

   // loop to read and output each entry in the directory
   while ($entry = $directory_handle->read()) {
      echo "Entry: " . $entry . "<br>";
   }

   // Rewind the directory handle to start reading from the beginning again
   $directory_handle->rewind();

   // Loop again to read and output each entry in the directory
   while ($entry = $directory_handle->read()) {
      echo "Entry after rewind: " . $entry . "<br>";
   }

   // Close the directory handle
   $directory_handle->close();
?> 

Output

This will generate the following output −

Directory Handle: Resource id #3
Directory Path: ./images/
Entry: .
Entry: ..
Entry: shankar.jpg
Entry: robot.jpg
Entry: hanuman.jpg
Entry after rewind: .
Entry after rewind: ..
Entry after rewind: shankar.jpg
Entry after rewind: robot.jpg
Entry after rewind: hanuman.jpg

Summary

The dir() function in PHP opens a directory and returns a handle that can be used to view its contents. Use read() to iterate over the entries. To close, use close() function.

So we can say that this function is best for basic directory operations.

php_function_reference.htm
Advertisements