PHP | opendir() Function Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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(). Syntax: opendir($path, $context) 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: Program 1: php <?php // Opening a directory $dir_handle = opendir("/user/gfg/docs/"); if(is_resource($dir_handle)) { echo("Directory Opened Successfully."); } // closing the directory closedir($dir_handle); else { echo("Directory Cannot Be Opened."); } ?> Output: Directory Opened Successfully. Program 2: php <?php // opening a directory and reading its contents $dir_handle = opendir("user/gfg/sample.docx"); if(is_resource($dir_handle)) { while(($file_name = readdir($dir_handle)) == true) { echo("File Name: " . $file_Name); echo "<br>" ; } // closing the directory closedir($dir_handle); } else { echo("Directory Cannot Be Opened."); } ?> Output: File Name: sample.docx Reference: https://www.php.net/manual/en/function.opendir.php Comment More infoAdvertise with us Next Article PHP finfo_open() Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-file-handling Practice Tags : Misc Similar Reads PHP | mkdir( ) Function The mkdir() creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure. The mode parameter in mkdir() function is ignored on Windows platforms. Syntax: mkdir(path, mode, recursive, context) 2 min read PHP | zip_open() Function The zip_open() function is an inbuilt function in PHP which is used to open a zip archive for reading. The zip_open() function creates a new stream and establishes a connection between the stream and a Zip Archive. The filename is sent as a parameter to the zip_open() function and it returns a valid 2 min read PHP finfo_open() Function The finfo_open() function is an inbuilt function in PHP that creates a new info instance. This function is in PHP 7 and 8. Syntax: Procedural Style:finfo_open(Flag, magic_database = null)Object-Oriented Style:public finfo::__construct(Flag, $magic_database = null)Parameters: This function has only t 1 min read PHP | getcwd( ) Function The getcwd() function in PHP is an inbuilt function which is used to return the current working directory. This function does not accepts any parameter and returns the current working directory on successful function call or FALSE on failure. Syntax: getcwd() Parameters: This function does not accep 2 min read PHP | ftp_mkdir() function The ftp_mkdir() function is an inbuilt function in PHP which is used to create a new directory on the ftp server. Once the directory is created cannot be created again. Creating a directory that already exists will produce error. Syntax: string ftp_mkdir( $ftp_connection, $directory_name ) Paramete 3 min read PHP | link( ) Function The link() creates a hard link for a specified target. The target and the link are passed as parameters to the link() function and it returns true on success and false on failure.Syntax: link(target, link)  Parameters Used: The link() function in PHP accepts two parameters. target : It is a manda 2 min read Like