How can the use of foreach() in PHP help in managing and displaying files in a directory?

When managing and displaying files in a directory in PHP, using a foreach loop can simplify the process by iterating through each file in the directory. This allows you to easily access and display information about each file, such as the file name, size, and modification date.

// Specify the directory path
$directory = "path/to/directory";

// Get all files in the directory
$files = scandir($directory);

// Iterate through each file using foreach
foreach($files as $file){
    // Display file information
    echo "File Name: " . $file . "<br>";
    echo "File Size: " . filesize($directory . "/" . $file) . " bytes<br>";
    echo "Last Modified: " . date("Y-m-d H:i:s", filemtime($directory . "/" . $file)) . "<br><br>";
}