You are here

public function SanitizeName::sanitizeFilename in Transliterate filenames for uploads 8

Sanitize the file name.

Parameters

string $filename: The file name that will be sanitized.

Return value

string Sanitized file name.

File

src/SanitizeName.php, line 40

Class

SanitizeName
Class SanitizeName.

Namespace

Drupal\transliterate_filenames

Code

public function sanitizeFilename($filename) {
  $filename = $this->transliteration
    ->transliterate($filename);

  // Replace whitespace.
  $filename = str_replace(' ', '-', $filename);

  // Remove remaining unsafe characters.
  $filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);

  // Remove multiple consecutive non-alphabetical characters.
  $filename = preg_replace('/(_)_+|(\\.)\\.+|(-)-+/', '\\1\\2\\3', $filename);

  // Force lowercase to prevent issues on case-insensitive file systems.
  $filename = strtolower($filename);
  return $filename;
}