You are here

function filefield_sources_clean_filename in FileField Sources 7

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

Clean up the file name, munging extensions and transliterating.

Parameters

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

Return value

A file path with a cleaned-up file name.

2 calls to filefield_sources_clean_filename()
filefield_source_attach_value in sources/attach.inc
A #filefield_value_callback function.
filefield_source_remote_value in sources/remote.inc
A #filefield_value_callback function.

File

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

Code

function filefield_sources_clean_filename($filepath, $extensions) {
  global $user;
  $filename = basename($filepath);
  if (module_exists('transliteration')) {
    module_load_include('inc', 'transliteration');
    $langcode = NULL;
    if (!empty($_POST['language'])) {
      $languages = language_list();
      $langcode = isset($languages[$_POST['language']]) ? $_POST['language'] : NULL;
    }
    $filename = transliteration_clean_filename($filename, $langcode);
  }

  // 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_dirname($filepath);
  return ($directory != '.' ? $directory . '/' : '') . $filename;
}