PHP readdir() Function: Reading Directory Entries
The PHP readdir() function reads the next entry from an opened directory handle, returning the filename in filesystem order, with details on its parameters, return values, and example code demonstrating correct and incorrect directory traversal techniques.
The PHP readdir() function reads the next entry from an opened directory handle and returns the filename in the filesystem's sorting order.
Parameters
dir_handle – a resource representing the directory handle opened previously by opendir() .
Return value
Returns the filename as a string on success, or FALSE on failure.
Example
<?php
// Note: the !== operator does not exist before version 4.0.0‑RC2
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Files:\n";
/* Correct way to iterate the directory */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* Incorrect way to iterate the directory */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
}
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.