How can a list of files in a server directory be obtained using PHP?
To obtain a list of files in a server directory using PHP, you can use the opendir() function to open the directory, readdir() function to read the directory contents, and closedir() function to close the directory. You can loop through the directory contents using a while loop and display each file name.
$dir = "/path/to/directory";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
echo "Filename: $file<br>";
}
}
closedir($dh);
}
}
Keywords
Related Questions
- How can the user prevent an extra line break from being added at the end of each message in the text file?
- What potential pitfalls should be avoided when handling checkbox values in PHP forms and updating them in a database?
- How can Dependency Injection be utilized to avoid the need for including helper classes in every PHP file?