Skip to content

Commit

Permalink
Skip UTF8 byte order mark in CSV files
Browse files Browse the repository at this point in the history
  • Loading branch information
mdurys committed May 20, 2024
1 parent f25246e commit a31883f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/DataSource/Interpreter/CsvFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

class CsvFileInterpreter extends AbstractInterpreter
{
private const UTF8_BOM = "\xEF\xBB\xBF";

/**
* @var bool
*/
Expand All @@ -43,6 +45,8 @@ class CsvFileInterpreter extends AbstractInterpreter
protected function doInterpretFileAndCallProcessRow(string $path): void
{
if (($handle = fopen($path, 'r')) !== false) {
$this->skipByteOrderMark($handle);

if ($this->skipFirstRow) {
//load first row and ignore it
$data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escape);
Expand Down Expand Up @@ -88,6 +92,8 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo
$readRecordNumber = -1;

if ($this->fileValid($path) && ($handle = fopen($path, 'r')) !== false) {
$this->skipByteOrderMark($handle);

if ($this->skipFirstRow) {
//load first row and ignore it
$data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure, $this->escape);
Expand Down Expand Up @@ -127,4 +133,11 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo

return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns);
}
private function skipByteOrderMark($handle): void
{
$bom = fread($handle, strlen(self::UTF8_BOM));
if (0 !== strncmp(self::UTF8_BOM, $bom, strlen(self::UTF8_BOM))) {
rewind($handle);
}
}
}
2 changes: 1 addition & 1 deletion tests/bin/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
PIMCORE_TEST: 1
PIMCORE_TEST_DB_DSN: "mysql://pimcore:pimcore@db:3306/pimcore_test"
depends_on:
- db
- db
volumes:
- ../../.:/var/www/html
- /var/www/html/vendor
Expand Down

0 comments on commit a31883f

Please sign in to comment.