How can opendir and readdir functions be utilized to read and display directory contents in PHP?
To read and display directory contents in PHP, you can use the opendir() function to open a directory handle and then use the readdir() function to read the contents of the directory one by one. You can loop through the directory contents and display them as needed.
$dir = "/path/to/directory";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
echo "filename: $file : filetype: " . filetype($dir . $file) . "<br>";
}
}
closedir($dh);
}
}
Keywords
Related Questions
- What are some common pitfalls when using variables in PHP queries, and how can they be avoided?
- In the context of PHP development, what are some recommended approaches for handling text manipulation tasks, such as counting words or splitting strings efficiently?
- What is the main issue the user is facing when trying to pass individual form data in a loop in PHP?