Reading Directory Entries in PHP


How To Read a Directory One Entry at a Time in PHP?



If you want to read a directory one entry at a time, you can use opendir() to open the specified directory to create a directory handle, then use readdir() to read the directory contents through the directory handle one entry at a time. readdir() returns the current entry located by the directory pointer and moves the pointer to the next entry. When end of directory is reached, readdir() returns Boolean false. Here is a PHP script example on how to use opendir() and readdir():

$array["one"] = "Download PHP scripts at\r\n";
$array["two"] = "Download Perl scripts at\r\n";
$bytes = file_put_contents("/temp/download/todo.txt", $array);
print("List of files:\n");
$dir = opendir("/temp/download");
while ( ($file=readdir($dir)) !== false ) {

This script will print:

List of files:


Retrieving Directory Name from File Path Name in PHP

Putting Directory Entries into an Array in PHP

Working with Directories and Files in PHP

⇑⇑ PHP Tutorials

2016-11-20, 1463🔥, 0💬