PHP List Files: Methods & Examples
- You can obtain a list of files that meet a specific pattern using the `glob()` function in PHP, for example:
$files = glob('path/to/directory/*');
foreach ($files as $file) {
echo $file . "<br>";
}
- You can use the scandir() function in PHP to get a list of files in a directory, for example:
$files = scandir('path/to/directory');
foreach ($files as $file) {
echo $file . "<br>";
}
- You can open a directory, read a list of files, and close the directory using the opendir(), readdir(), and closedir() functions in PHP.
$dir = 'path/to/directory';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo $file . "<br>";
}
}
closedir($handle);
}
All of these methods can be used to obtain a list of files in a directory. Choose the method that best suits your needs for obtaining a file list.