write utf-8 characters to file with fputcsv in php

write utf-8 characters to file with fputcsv in php

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

Stefan Bogdanescu

Founder & Senior Architect

Handling UTF-8 with fputcsv in PHP

The core issue you are facing isn't a bug in the fputcsv() function itself; it is a mismatch between how the file is encoded and how the reader expects to interpret that data. When you open a file for writing using fopen($filepath, 'w'), PHP does not automatically set an encoding standard. It writes raw bytes based on your system's default locale or the specific character set of the string being passed.

If your input strings contain Persian characters (which are UTF-8), and your output file remains in a legacy encoding like Windows-1256 (often the default for Persian locales) or ISO-8859-1, the resulting CSV will look like garbage to any standard spreadsheet application. The bytes representing "Hello" in UTF-8 are completely different from the bytes representing "حبا" in a Latin-based encoding. To fix this, you must explicitly instruct PHP to handle the output as UTF-8 before writing the first row.

The most robust way to ensure fputcsv() outputs valid UTF-8 is to set the internal buffer state using mb_internal_encoding(). While this function primarily affects multibyte string functions like strlen or substr, it effectively sets the encoding context for the current script, ensuring that when you pass a UTF-8 string to fputcsv(), it treats the characters as multi-byte sequences rather than trying to map them to single-byte equivalents.

Here is how you should modify your code:

// Ensure the internal multibyte encoding is set to UTF-8
mb_internal_encoding('UTF-8');

$filepath = 'data.csv';
$handle = fopen($filepath, 'w');

// It is best practice to write a BOM (Byte Order Mark) for UTF-8
// especially when targeting Windows-based viewers like Excel.
// However, fputcsv does not handle BOMs automatically on the first write.
// You must prepend the BOM manually if you need Excel compatibility.

// Optional: Write UTF-8 BOM for Excel/Windows compatibility
if (version_compare(PHP_VERSION, '7.0', '>=')) {
    fwrite($handle, "\xEF\xBB\xBF");
}

fputcsv($handle, array($coupon->code, $discount->label));
fclose($handle);

There is a critical nuance regarding the Byte Order Mark (BOM). Excel on Windows often fails to display UTF-8 text correctly unless it detects the BOM (\xEF\xBB\xBF) at the very beginning of the file. Without this mark, Excel will attempt to read the UTF-8 bytes using an ASCII or ANSI table, resulting in mojibake (garbled characters). If your audience uses macOS or Linux applications like LibreOffice Calc or Google Sheets, the BOM is actually undesirable as it can cause parsing errors. You must decide based on your target environment whether to include the BOM manually before calling fputcsv().

Another common pitfall involves the delimiter and enclosure settings. By default, fputcsv() uses a comma as the field separator and double quotes for enclosure. If your CSV contains commas within the data (e.g., "Discount: 50% off"), these characters need to be enclosed in quotes to prevent column misalignment. The function handles this automatically if you pass an array, but it does not handle non-UTF-8 encodings well without the mb_internal_encoding() context set first.

If you are working within a Laravel project, consider using the Storage facade or a dedicated CSV package that abstracts these encoding headaches away. Libraries like maatwebsite/excel or phpoffice/phpspreadsheet handle BOM injection and UTF-8 detection internally, reducing the risk of manual errors in your core logic.

Finally, always verify the output by opening the file with a text editor (like VS Code or Notepad++) rather than a spreadsheet application first. This allows you to inspect the raw hex values and confirm that the UTF-8 BOM is present if required, ensuring that your data integrity remains intact before distribution.

Our network