function FileEventSubscriber::ensureValidUtf8Filename

Guarantees a valid UTF-8 filename for subsequent event handlers.

Parameters

\Drupal\Core\File\Event\FileUploadSanitizeNameEvent $event: File upload sanitize name event.

File

core/modules/file/src/EventSubscriber/FileEventSubscriber.php, line 58

Class

FileEventSubscriber
Sanitizes uploaded filenames.

Namespace

Drupal\file\EventSubscriber

Code

public function ensureValidUtf8Filename(FileUploadSanitizeNameEvent $event) : void {
  $filename = $event->getFilename();
  if (!mb_check_encoding($filename, 'UTF-8')) {
    $replacement = $this->configFactory
      ->get('file.settings')
      ->get('filename_sanitization.replacement_character');
    $event->setFilename(self::coerceToValidUtf8($filename, $replacement));
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.