How can PHP be used to offer CSV files for download to clients?
To offer CSV files for download to clients using PHP, you can create a PHP script that sets the appropriate headers to indicate that the content being served is a CSV file and then outputs the CSV data. This allows clients to download the file directly from the browser.
<?php
// Set headers to indicate that the content is a CSV file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="data.csv"');
// Output CSV data
echo "Name,Email\n";
echo "John Doe,johndoe@example.com\n";
echo "Jane Smith,janesmith@example.com\n";
echo "Bob Johnson,bobjohnson@example.com";
?>
Related Questions
- What potential issues can arise when switching between MySQLi and PDO in PHP code?
- What are some best practices for updating database records in PHP when a user interacts with a forum thread?
- Are there any best practices or universal methods for implementing file downloads in PHP that are independent of file type?