What are the limitations of using a while loop to send multiple files to the browser in PHP?

Using a while loop to send multiple files to the browser in PHP may lead to performance issues or memory exhaustion if the files are large or numerous. To solve this issue, you can use output buffering to store the file contents in memory before sending them to the browser.

<?php
// List of file paths to send to the browser
$files = ['file1.txt', 'file2.txt', 'file3.txt'];

// Start output buffering
ob_start();

// Loop through each file and send its contents to the buffer
foreach ($files as $file) {
    readfile($file);
}

// Get the contents of the output buffer
$contents = ob_get_clean();

// Send the contents to the browser
echo $contents;