mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #15947 from manuzoli/fix/csv-import-encoding
Fix: Better detection of encoding for .csv files
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user