Merge pull request #15947 from manuzoli/fix/csv-import-encoding

Fix: Better detection of encoding for .csv files
This commit is contained in:
Dan Harrin
2025-04-02 08:53:49 +01:00
committed by GitHub
@@ -439,7 +439,21 @@ trait CanImportRecords
protected function detectCsvEncoding(mixed $resource): ?string
{
$fileHeader = fgets($resource);
rewind($resource);
$lineCount = 0;
$contentSample = '';
while ((! feof($resource)) && ($lineCount < 20)) {
$line = fgets($resource);
if ($line === false) {
break;
}
$contentSample .= $line;
$lineCount++;
}
// The encoding of a subset should be declared before the encoding of its superset.
$encodings = [
@@ -454,7 +468,7 @@ trait CanImportRecords
];
foreach ($encodings as $encoding) {
if (! mb_check_encoding($fileHeader, $encoding)) {
if (! mb_check_encoding($contentSample, $encoding)) {
continue;
}