PHP | dir() Function Last Updated : 11 Jul, 2018 Comments Improve Suggest changes Like Article Like Report 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. Syntax: dir($directory, $context) 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: Program 1: php <?php $dir_handle = dir("user/gfg"); while(($file_name = $dirhandle->read()) !== false) { echo("File Name : " . $file_name); echo "<br>" ; } ?> Output: File Name: gfg.jpg File Name: .. File Name: gfg.pdf File Name: . File Name: gfg.txt Program 2: php <?php $dir_handle = dir("user/gfg"); echo("Directory Path: " . $dir_handle->path . "<br>"); echo("Directory Handler ID: " . $dir_handle->handle . "<br>"); while(($file_name = $dir_handle->read()) !== false) { echo("File Name: " . $file_name); echo "<br>" ; } $dir_handle->close(); ?> Output: 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 Reference: http://php.net/manual/en/function.dir.php Comment More infoAdvertise with us Next Article PHP | dir() Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-file-handling Practice Tags : Misc Similar Reads PHP chdir() Function PHP chdir() function is used to change the current directory to new directory path. It takes only a single argument as new directory path. Syntaxbool chdir(string $new_directory_path)ParametersThis function accepts one parameter, which is mandatory. Return ValueIt returns a boolean operator as retur 2 min read PHP | is_dir( ) Function The is_dir() function in PHP used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else it returns False. Syntax: is_dir($file) Parameters Used: The is_dir() function in PHP 1 min read PHP | closedir( ) Function The closedir() function in PHP is an inbuilt function which is used to close a directory handle. The directory handle to be closed is sent as a parameter to the closedir() function and the closedir() closes the directory handle. The directory handle must be previously opened by the opendir() functio 2 min read PHP | ftp_chdir() function The ftp_chdir() function is an inbuilt function in PHP which is used to change the current directory on the FTP server. Syntax: ftp_chdir( $ftp_connection, $directory ) Parameter: This function accepts two parameters as mentioned above and described below:  $ftp_connection: It is required paramete 2 min read PHP | chroot( ) Function The chroot() function in PHP is an inbuilt function which is used to change the root directory of the current process to directory. The chroot() function changes the current working directory to "/". The chroot() function is only available to GNU and BSD systems, and only when the user is using the 2 min read PHP fileowner() Function The fileowner() is an inbuilt function in PHP that returns the details for the file owner. Syntax: fileowner(string $filename): int|falseParameters: This function accepts a single parameter: filename: This parameter specifies the file path for the specific file.Name of the file. Return Value: The us 1 min read PHP | filetype( ) Function The filetype() function in PHP is an inbuilt function which is used to return the file type of a specified file or a directory. The filetype() function accepts the filename as a parameter and returns one of the seven file types on success and False on failure. The seven possible return values of the 2 min read PHP | ftell( ) Function The ftell() function in PHP is an inbuilt function which is used to return the current position in an open file. The file is sent as a parameter to the ftell() function and it returns the current file pointer position on success, or FALSE on failure.Syntax:  ftell( $file ) Parameters Used: The ftel 2 min read PHP fileinode() Function The fileinode() function is an inbuilt function in PHP that returns the inode of the file. Syntax: fileinode(string $filename): int|falseParameter: This function has only one parameter: filename: This parameter specifies the path for the particular file.Return Value: This function returns the inode 1 min read PHP fpassthru( ) Function The fpassthru() function in PHP is an inbuilt function which is used to read data from a current position from a specified file until end of file and then write the result to the output buffer. The file which has to be read is sent as a parameter to the fpassthru() function and it returns the number 2 min read Like