PHP | readdir() Function Last Updated : 17 Jul, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The readdir() function in PHP is an inbuilt function which is used to return the name of the next entry in a directory. The method returns the filenames in the order as they are stored in the filenamesystem. The directory handle is sent as a parameter to the readdir() function and it returns the entry name/filename on success or False on failure. Syntax: readdir(dir_handle) Parameters Used: The readdir() function in PHP accepts one parameter. dir_handle : It is a mandatory parameter which specifies the handle resource previously opened by the opendir() function. Return Value: It returns the entry name/filename on success, or False on failure. Errors And Exceptions: If the directory handle parameter is not specified by the user then the last link opened by opendir() is assumed by the readdir() function. Apart from returning Boolean FALSE, the readdir() function may sometimes also return a non-Boolean value which evaluates to FALSE. Below programs illustrate the readdir() function: Program 1: php <?php // opening a directory $dir_handle = opendir("user/gfg/"); // reading the contents of the directory while(($file_name = readdir($dir_handle)) !== false) { echo("File Name: " . $file_name); echo "<br>" ; } // closing the directory closedir($dir_handle); ?> Output: File Name: gfg.jpg File Name: .. File Name: article.pdf File Name: . File Name: article.txt Program 2: php <?php // opening a directory $dir_handle = opendir("user/gfg/"); if(is_resource($dir_handle)) { // reading the contents of the directory while(($file_name = readdir($dir_handle)) !== false) { echo("File Name: " . $file_name); echo "<br>" ; } // closing the directory closedir($dir_handle); } else { echo("Failed to Open."); } } else { echo("Invalid Directory."); } ?> Output: File Name: gfg.jpg File Name: .. File Name: article.pdf File Name: . File Name: article.txt Reference : http://php.net/manual/en/function.readdir.php Comment More infoAdvertise with us Next Article PHP | zip_read() Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-file-handling PHP-function +1 More Practice Tags : Misc Similar Reads PHP | rewinddir() Function The rewinddir() function is an inbuilt function in PHP which is used to rewind the directory handle. The rewinddir() function opens a directory and list its files, resets the directory handle, list its files once again, then finally closes the directory handle. The directory handle sent as a paramet 2 min read PHP | rmdir( ) Function The rmdir() function in PHP is an inbuilt function which is used to remove an empty directory. It is mandatory for the directory to be empty, and it must have the relevant permissions which are required to delete the directory. The directory to be deleted is sent as a parameter to the rmdir() functi 2 min read PHP readfile( ) Function The readfile() function in PHP reads a file and writes its contents directly to the output buffer (usually the browser). Itâs an easy way to show a file without reading it line by line yourself.If thereâs a problem reading the file, like if it doesnât exist, PHP will usually show an error message. T 3 min read PHP | zip_read() Function The zip_read() function is an inbuilt function in PHP which is used to read an entity present in the opened zip archive. The zip resource is to be read and sent as parameters to the zip_read() function and it returns a resource containing file within the zip archive on success, or FALSE if there is 2 min read PHP scandir( ) Function In this article, we will see how to get all the files from the current or specified directory using the scandir() function in PHP. The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists th 3 min read PHP | XMLReader read() Function The XMLReader::read() function is an inbuilt function in PHP which is used to move to next node in document. Thus this function is used to traverse through the XML document. Syntax: bool XMLReader::read( void ) Parameters: This function doesnât accepts any parameter. Return Value: This function retu 2 min read Like