What are some best practices for generating and offering a text file for download in PHP?

When generating and offering a text file for download in PHP, it is important to set the appropriate headers to indicate that the response is a file download. This includes setting the Content-Type to "text/plain" and using the Content-Disposition header to specify the filename. Additionally, the file contents should be output using functions like file_get_contents() or readfile().

<?php
// Set the headers for a text file download
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="example.txt"');

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