PHP Directory closedir() Function (With Examples)

In this tutorial, we will learn about the PHP closedir() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP closedir() function

The full form of closedir is "Close Directory", the function closedir() is used to close an opened directory.

Syntax

The syntax of the closedir() function:

closedir(dir_handle);

Parameters

The parameters of the closedir() function:

  • dir_handle – it is an optional parameter which is used to specify the directory handle resource if we do not specify the directory handle resource – it assumes the last link opened with opendir() function.

Return Value

It returns nothing.

PHP closedir() Function Example

PHP code to open a directory, read its files, and close it.

<?php

$path = "/home";

//checking whether $path is a directory or not
//then, opening the directory and reading its files
if (is_dir($path)) {
    if ($dh = closedir($path)) {
        while (($file = readdir($dh)) !== false) {
            echo "File:" . $file . "<br/>";
        }
        //closing the directory
        closedir($dh);
    }
}
?>

Output

The output of the above example is:

File:..
File:main.php
File:.

Reference: PHP closedir() function

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.