How can PHP be optimized to streamline the process of generating and offering files for download?

Issue: PHP file downloads can be slow and resource-intensive if not optimized. To streamline the process, we can use output buffering to efficiently serve files for download.

<?php
// Set headers for file download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.txt"');

// Open and output file using output buffering
ob_start();
readfile('path/to/example.txt');
ob_end_flush();