id,name,email,created_at
1,Alice,alice@example.com,2025-01-15
2,Bob,"Doe, Bob",2025-02-20
Values containing commas or quotes are wrapped in double quotes. Quotes inside a value are doubled: "".
// Reading
$file = fopen('users.csv', 'r');
while (($row = fgetcsv($file)) !== false) {
[$id, $name, $email] = $row;
}
fclose($file);
// Writing
$file = fopen('export.csv', 'w');
fputcsv($file, ['id', 'name', 'email']); // header
fputcsv($file, [1, 'Alice', 'alice@example.com']);
fclose($file);
Encoding: Excel opens files in Windows-1252 by default, not UTF-8. Add a BOM (\xEF\xBB\xBF) at the start of the file for Excel to display Cyrillic correctly.