Today, when I was re-enabling a custom migration module again after a few months, it stopped working because a few files contain special german characters like äöüÄÖÜß. I haven’t changed anything in my code, but I updated Drupal from 9.1 to 9.3 recently.
In my module, I scan a directory for its files in the module’s directory and then copy these files to the site’s public file directory. When logging error messages, it seems like the filesystem isn’t able to understand the file’s names correctly: Invalid filename 'df▒d93.JPG'
. The file’s correct name is dföd93.JPG
.
The function Drupal::service('file_system')->copy($file->uri, $path . $file->filename, FileSystemInterface::EXISTS_REPLACE);
makes a check if the file name is valid UTF-8 (Unicode::validateUtf8()
), so for the filename with the ▒
character, it throws an error.
But I think the problem might lie at the scanDirectory()
function which returns the wrong file names in the first place:
Drupal::service('file_system')->scanDirectory($dirname, '/.*/', [ 'recurse' => TRUE, 'key' => 'uri', ]);
Has anybody experienced the same problem? Any help is appreciated.