How can I output a UTF-8 CSV in PHP that Excel will read properly?

How can I output a UTF-8 CSV in PHP that Excel will read properly?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Invisible Barrier: Why Excel Fails with UTF-8 CSVs

When you set a Content-Type header to application/csv;charset=UTF-8, you are signaling to the browser that the payload contains Unicode data. This is technically correct for modern web standards, but it hits a hard wall when Microsoft Excel opens the file. Excel treats the input as a specific legacy format and ignores the charset declaration in the HTTP headers entirely. When Excel encounters a UTF-8 encoded file without a Byte Order Mark (BOM), it assumes the data is ISO-8859-1 (Latin-1). This mismatch causes characters outside the ASCII range to be misinterpreted, resulting in the infamous "íÄ" garbage text you are seeing.

The solution isn't just about telling Excel how to read the file; it's about giving Excel a visual cue that tells it exactly which encoding to expect. That cue is the BOM.

Implementing the BOM in PHP

You attempted to output the BOM using echo pack("CCC", 0xef, 0xbb, 0xbf);. While the binary data is correct, the placement and method of output can sometimes cause issues depending on how the server streams the response or if buffering interferes with the headers. However, simply echoing those bytes at the very start of your script usually works for generating a file download.

To make this robust in a dynamic environment where you are streaming data directly to the user without writing to disk first, prepend the BOM immediately after sending your headers and before any whitespace. Whitespace is critical here; if there is even a single newline character between the PHP output start and the three bytes (0xEF, 0xBB, 0xBF), Excel may fail to recognize the marker.

Here is how you should structure the output phase:

// Set headers first
header("Content-Type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=\"CHS.csv\"");

// Output the BOM immediately
echo "\xEF\xBB\xBF"; 

// Now proceed to echo your CSV data
// header("Row 1");
// echo "Name,Value\n"; 

Using the hex escape sequences "\xEF\xBB\xBF" is often cleaner than packing individual bytes, ensuring the characters are interpreted correctly by PHP as a string before being sent to the client.

The Header Row Constraint

A common pitfall when mixing BOMs with CSV data is where you place the first row of your actual data. If you echo a header row like header("Row 1") before echoing the BOM, Excel will treat that text as part of the file signature, corrupting the import process. The BOM must be the very first byte sent to the client, preceding any content.

Additionally, while modern versions of Excel can handle UTF-8 without a BOM if configured correctly via settings (like File > Options > Advanced > Edit Custom Lists or specific regional settings), this behavior is inconsistent. Relying on user configuration changes is not a viable support strategy for public-facing applications. The BOM approach guarantees compatibility across different Windows locales and Excel versions, specifically the Mac 2008 version mentioned in your scenario which lacks easy UTF-8 import options.

Alternative: Writing to Disk First

If you find that streaming the BOM directly causes buffering issues or if your application architecture separates logic from output significantly, generating the CSV to a temporary file is a reliable fallback. You can write the BOM to the start of that file using standard file I/O functions (file_put_contents), ensuring the bytes are written atomically before sending the file path in a download link or forcing the browser to treat it as a local file import.

$tempFile = tempnam(sys_get_temp_dir(), 'CSV_');

// Write BOM and data to file
file_put_contents($tempFile, "\xEF\xBB\xBF" . $csvContent);

// Force download
header("Content-Type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=\"CHS.csv\"");
readfile($tempFile);

// Cleanup
unlink($tempFile);

This method removes any ambiguity about stream buffering and ensures the BOM is strictly the first bytes on disk. While it adds a small overhead for file creation and deletion, the reliability gain often outweighs the cost for critical data exports involving non-ASCII characters.

Our network