The opendir() function in PHP is an inbuilt function which is used to open a directory handle. The path of the directory to be opened is sent as a parameter to the opendir() function and it returns a directory handle resource on success, or FALSE on failure.
The opendir() function is used to open up a directory handle to be used in subsequent with other directory functions such as closedir(), readdir(), and rewinddir().
Parameters Used: The opendir() function in PHP accepts two parameters.
- $path : It is a mandatory parameter which specifies the path of the directory to be opened.
- $context : It is an optional parameter which specifies the behavior of the stream.
Return Value: It returns a directory handle resource on success, or FALSE on failure.
Errors And Exceptions:
- A PHP error of level E_WARNING is generated and opendir() returns FALSE if the path is not a valid directory or the directory cannot be opened due to permission restrictions or filesystem errors.
- The error output of opendir() can be suppressed by prepending ‘@’ to the front of the function name.
Below programs illustrate the opendir() function:
Directory Opened Successfully.
File Name: sample.docx
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.