Home » PHP

PHP dir() function with example

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

PHP dir() function

dir() function is an instance of the directory class, it is used to read the directory, it includes handle and path properties – which can be used to get the resource id and path to the directory. Both handle and path have read(), rewind(), and close() methods.

Syntax:

    dir(directory,context);

Parameter(s):

  • directory – It is used to specify the path to the directory to be opened.
  • context – It is an optional parameter; it defines the context (a set of options that can modify the behavior of the stream).

Return value:

On success – it returns an instance to the directory, on fail – it returns "FALSE".

Example: PHP code to demonstrate example of dir() function

<?php 
//path to current working directory
$cwd = dir(getcwd());

echo "Directory handle: " . $cwd->handle . "<br>";
echo "Current path: " . $cwd->path . "<br>";

//reading & printing the filenames using dir()
while (($file = $cwd->read()) !== false){ 
  echo "File: " . $file . "<br>"; 
} 

//closing the dir instance
$cwd->close(); 
?>

Output

Directory handle: Resource id #5
Current path: /home
File: .
File: ..
File: main.php

Reference: PHP dir() function







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




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.