Home » PHP

PHP closedir() function with example

PHP closedir() function: Here, we are going to learn about the PHP closedir() function with its syntax, parameters, returns type, and example.
Submitted by IncludeHelp, on August 13, 2019

PHP closedir() function

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

Syntax:

    closedir(dir_handle);

Parameter(s):

  • 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.

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

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

Reference: PHP closedir() function






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.