What are best practices for ensuring that CSV/XLS files are downloaded instead of displayed in the browser?
When serving CSV/XLS files from a PHP script, it's important to set the appropriate headers to ensure that the file is downloaded instead of displayed in the browser. This can be achieved by setting the Content-Type header to 'application/octet-stream' and Content-Disposition header to 'attachment'. Additionally, it's recommended to include the file name in the Content-Disposition header to prompt the user to save the file with the correct name.
<?php
// Set the CSV/XLS file path
$file = 'path/to/your/file.csv';
// Set the appropriate headers for file download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
// Output the file to the browser
readfile($file);