What are common issues when exporting a CSV file from a PHP web application?
One common issue when exporting a CSV file from a PHP web application is that special characters or commas in the data can cause the CSV file to be formatted incorrectly. To solve this issue, you can use the fputcsv function in PHP, which automatically handles special characters and commas in the data.
// Create a file pointer
$fp = fopen('export.csv', 'w');
// Write column headers
$headers = array('Name', 'Email', 'Phone');
fputcsv($fp, $headers);
// Write data rows
$data = array(
array('John Doe', 'john.doe@example.com', '555-1234'),
array('Jane Smith', 'jane.smith@example.com', '555-5678')
);
foreach ($data as $row) {
fputcsv($fp, $row);
}
// Close the file pointer
fclose($fp);