Gen. partnerAlgotech

Sending a CSV file

Therefore, send the headers as follows:

|
December 18, 2022

When sending binary files, always think about what HTTP headers to choose. In case of sending a CSV file (almost ideal format for simple text tables, which can be processed by Excel), Content-Type: application/csv, in UTF-8 encoding is useful.

However, in some versions of Excel there is a problem with UTF-8 encoding. To make sure that the correct encoding is detected, we need to insert the UTF-8 BOM, which is a special character xEF\xBB\xBF that tells the client that it is UTF-8, since it does not exist in any other encoding.

Therefore, send the headers as follows:

header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=' . date('d-m-y') . '_file.csv');
header('Pragma: no-cache');
echo "\xEF\xBB\xBF";
Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.