The dir() function in PHP is an inbuilt function which is used to return an instance of the Directory class. The dir() function is used to read a directory, which includes the following:
- The given directory is opened.
- The two properties handle and path of dir() are available.
- Both handle and path properties have three methods: read(), rewind(), and close().
The path of the directory is sent as a parameter to the opendir() function and it returns an instance of the Directory class on success, or FALSE on failure.
Parameters Used: The dir() function in PHP accepts two parameters as described below.
- $directory: It is a mandatory parameter which specifies the path of the directory.
- $context: It is an optional parameter which specifies the behavior of the stream.
Return Value: It returns an instance of the Directory class on success, or FALSE on failure.
Errors And Exceptions:
- A NULL value is returned if the dir() is passed with wrong parameters.
- The order in which directory entries are returned by the read method is system-dependent.
Below programs illustrate the dir() function:
File Name: gfg.jpg File Name: .. File Name: gfg.pdf File Name: . File Name: gfg.txt
Directory Path: user/gfg Directory Handler ID: Resource id #2 File Name: gfg.jpg File Name: .. File Name: gfg.pdf File Name: . File Name: gfg.txt
- PHP | each() Function
- PHP | Ds\Map map() Function
- PHP | Ds\Set first() Function
- p5.js | int() function
- PHP | Ds\Map last() Function
- p5.js | abs() function
- p5.js | box() Function
- D3.js | d3.set.add() Function
- PHP | Ds\Set add() Function
- PHP | key() Function
- PHP | min( ) Function
- p5.js | hex() function
- PHP | max( ) Function
- PHP | pos() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.