How can a loop be used to efficiently read an entire directory in PHP and store the file names in an array?
To efficiently read an entire directory in PHP and store the file names in an array, you can use a loop along with the `scandir()` function. The `scandir()` function reads the content of a directory and returns an array of file names. You can loop through this array and filter out any unwanted files (like `.` and `..`) to only store the desired file names in a separate array.
$directory = "/path/to/directory";
$files = array();
// Read directory
$files_in_directory = scandir($directory);
// Loop through files and store only file names
foreach($files_in_directory as $file) {
if($file != "." && $file != "..") {
$files[] = $file;
}
}
// Display the array of file names
print_r($files);