You are here

function filefield_sources_clean_filename in FileField Sources 8

Same name and namespace in other branches
  1. 6 filefield_sources.module \filefield_sources_clean_filename()
  2. 7 filefield_sources.module \filefield_sources_clean_filename()

Clean up the file name, munging extensions and transliterating.

Parameters

string $filepath: A string containing a file name or full path. Only the file name will actually be modified.

Return value

string A file path with a cleaned-up file name.

2 calls to filefield_sources_clean_filename()
Attach::value in src/Plugin/FilefieldSource/Attach.php
Value callback for file field source plugin.
Remote::value in src/Plugin/FilefieldSource/Remote.php
Value callback for file field source plugin.

File

./filefield_sources.module, line 585
Extend FileField to allow files from multiple sources.

Code

function filefield_sources_clean_filename($filepath, $extensions) {
  $filename = basename($filepath);
  $language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $filename = \Drupal::transliteration()
    ->transliterate($filename, $language);

  // Because this transfer mechanism does not use file_save_upload(), we need
  // to manually munge the filename to prevent dangerous extensions.
  // See file_save_upload().
  if (empty($extensions)) {
    $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
  }
  $filename = file_munge_filename($filename, $extensions);
  $directory = \Drupal::service('file_system')
    ->dirname($filepath);
  return ($directory != '.' ? $directory . '/' : '') . $filename;
}