What are some alternative methods to readdir() for reading files from a directory in PHP?
The readdir() function is deprecated in PHP 7.2 and removed in PHP 7.4, so it is recommended to use alternative methods to read files from a directory. One alternative method is to use the scandir() function, which returns an array of all files and directories in a directory. Another option is to use the glob() function, which allows for pattern matching to filter files in a directory.
// Using scandir() to read files from a directory
$files = scandir('/path/to/directory');
foreach($files as $file) {
if ($file != '.' && $file != '..') {
echo $file . "\n";
}
}
// Using glob() with pattern matching to read files from a directory
$files = glob('/path/to/directory/*');
foreach($files as $file) {
echo $file . "\n";
}