Closed Rahmon closed 7 months ago
I would appreciate it if you investigated this issue and opened a PR yourself with your findings if possible.
Adding a stream filter in the CanImportRecords
trait, getUploadedFileStream
method:
use League\Csv\CharsetConverter;
public function getUploadedFileStream(TemporaryUploadedFile $file) {
CharsetConverter::register();
$filePath = $file->getRealPath();
if (config('filament.default_filesystem_disk') !== 's3') {
$resource = fopen($filePath, mode: 'r');
$filter = stream_filter_append(
$resource,
CharsetConverter::getFiltername('iso-8859-15', 'utf-8'),
STREAM_FILTER_READ
);
return $resource;
}
// ...
}
makes the import work:
However, the input encoding would have to be set dynamically. This function doesn't seem to work, as of PHP8.1, to automatically detect encoding. Maybe a dropdown with possible encodings could be presented to the user instead?
Hi @valentin-morice
I tested your approach and it worked as expected.
Maybe instead of a dropdown with possible encodings presented to the user, it could be an option in the Importer class with the expected charsets and using the function mb_detect_encoding to detect the most likely character encoding.
Package
filament/actions
Package Version
v3.2.60
Laravel Version
v10.48.4
Livewire Version
v3.4.9
PHP Version
PHP 8.3.3
Problem description
When I try to import a CSV file encoded with ISO-8859-1, the columns are not mapped although I set up it in the getColumns method.
Even selecting the correct columns in the interface, I get the exception
Unable to encode attribute [data] for model [Filament\Actions\Imports\Models\FailedImportRow] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded.
Also, I noticed that the row data has the same issue. The expected output should be
Conceição
instead ofConceio
.In this case, for test purposes, I am able to fix the row data by updating the method Filament\Actions\Imports\Jobs\ImportCsv::utf8Encode from
to
But the title column remains with the issue and I am not able to import.
Other than that, I converted my test file to UTF-8 (
iconv -f ISO-8859-1 -t utf-8 input-ISO-8859-1.csv -o input-utf8.csv
) and it works as expected.Expected behavior
I expect that the Filament handles CSV files that are not encoded with UTF-8.
Steps to reproduce
guess()
input-ISO-8859-1.csv
in the project's root). At least one title should have a character likeç
,í
andã
.Reproduction repository
https://github.com/Rahmon/filamentphp-issues
Relevant log output