What is the limitation of outputting multiple files using PHP headers?

When outputting multiple files using PHP headers, the limitation is that headers must be sent before any output is displayed. To overcome this limitation, you can use output buffering to capture the output and then send the headers.

<?php
ob_start();

// Generate file content here

// Set headers for the first file
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file1.pdf"');

// Output the first file
echo $file1_content;

// Clear the output buffer
ob_clean();

// Set headers for the second file
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file2.pdf"');

// Output the second file
echo $file2_content;

// Flush the output buffer
ob_end_flush();
?>