What potential benefits and drawbacks are there in offering a file download directly from a PHP script instead of creating a temporary file?

When offering a file download directly from a PHP script instead of creating a temporary file, the potential benefits include reduced server storage usage and improved security by not leaving temporary files on the server. However, drawbacks may include increased server load due to generating the file on-the-fly and potential issues with large file downloads.

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

// Output file content directly
echo "This is the content of the file.";
?>