Decoding UTF-8 Corruption in Excel Downloads from PHP
You are encountering a classic text encoding mismatch between how your browser renders content and how the client application (Excel) interprets binary data. The symptomâseeing Désçàui instead of Désçà uiâis not a bug in PHP's output buffering; it is a direct result of a character set collision during the download process.
In your first example, you explicitly set Content-Type: application/vnd.ms-excel; charset=utf-8. While this header attempts to inform the client that the body contains UTF-8 bytes, Microsoft Excel does not reliably respect the charset parameter within the Content-Type header when dealing with binary file formats like .xls. More critically, the legacy Excel format (.xls) is fundamentally a binary container based on the DIF (Delimited Interchange Format), which historically assumes a code page of 1252 (Western European Windows) rather than UTF-8.
When you switch to Content-Type: text/html; charset=utf-8, the browser treats the response as an HTML document. The browser's rendering engine reads the UTF-8 bytes directly and displays them correctly because it knows how to map those bytes to Unicode characters. However, once you trigger the download in your first scenario, Excel receives the raw binary stream. It attempts to parse this stream assuming a default code page (usually Windows-1252).
Here is what happens under the hood:
- Your PHP script outputs bytes for
éas0xC3 0xA9. - Excel expects these bytes to represent standard ASCII or Code Page 1252 characters.
- In Code Page 1252, the byte
0xC3corresponds to the characterÃ, and0xA9corresponds to©(or similar depending on specific legacy mappings). - This misinterpretation cascades through the string, resulting in the garbled text
Désçàui.
The header Content-Disposition: attachment; filename=Test.xls instructs the browser to save the file rather than render it, but it does not change how Excel interprets the internal structure of the .xls file once it is opened. The extension .xls is the real culprit here, as it enforces a binary schema incompatible with raw UTF-8 strings without specific encoding layers.
To resolve this without relying on unsupported Content-Type directives for binary formats, you have two primary architectural paths:
Option 1: Use OpenXML (.xlsx)
The modern replacement for .xls, the .xlsx format, is based on the Office Open XML standard. It stores data as compressed ZIP archives containing XML files. Unlike the legacy .xls format, .xlsx supports UTF-8 natively in its internal XML structure.
You can adjust your PHP headers to serve an XML-based file:
ob_start();
echo 'Désçà ui'; // Ensure this is valid UTF-8
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=Test.xlsx");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
ob_end_flush();
By switching the extension to .xlsx and using the correct MIME type (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet), Excel will parse the XML content correctly, preserving your UTF-8 characters. This is often the preferred modern solution if you are generating new spreadsheets.
Option 2: Force Code Page Encoding
If you must strictly deliver a .xls file and cannot migrate to OpenXML, you must manually encode the string before sending it to match Excel's expectations. You need to convert your UTF-8 string into Code Page 1252 (or CP437) bytes before echoing it.
$original = 'Désçà ui';
// Convert UTF-8 to Code Page 1252 (Western European Windows)
$encoded = iconv('UTF-8', 'Windows-1252', $original);
ob_start();
echo $encoded;
header("Content-Type: application/vnd.ms-excel"); // Note: charset parameter is ignored by Excel for .xls
header("Content-Disposition: attachment; filename=Test.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
ob_end_flush();
In this approach, iconv translates the Unicode characters into the specific byte sequence Excel expects for that code page. When Excel opens the file, it reads those bytes and correctly maps them back to the visible characters in its UI. Be aware that this limits your character set to Western European languages; you lose support for characters outside the CP1252 range unless you use a different legacy format like .csv with proper BOM handling or switch to OpenXML.
Why Headers Alone Fail
Relying solely on header("Content-Type: ...; charset=utf-8") to force Excel to read UTF-8 in an .xls file is technically unsound. The MIME type tells the browser what to do, but the binary format of the file itself dictates how the client application (Excel) parses the data. Unless the file format specification explicitly supports UTF-8 (which .xls does not), you must pre-process the data or change the file format extension.
For robust applications handling international data, migrating to OpenXML (.xlsx) is generally superior. It aligns with modern web standards where UTF-8 is the default encoding for text-based formats, reducing the risk of character corruption during the download process. If you are using a framework like Laravel, ensure your output buffering settings do not interfere with these headers, though PHP's standard ob_* functions handle this well as long as headers are sent before any body content.