This project is not covered by Drupal’s security advisory policy.

Filename max length adds a maximum filename length to the filename sanitization options of Drupal core, and provides a Drush command to apply these options to files already on your site.

Long filenames are a common source of trouble: paths that exceed file system or archive limits, broken downloads, unreadable URLs. Core can transliterate filenames, replace whitespace and lowercase them, but it cannot limit their length. This module fills that gap.

Features

  • A Maximum filename length setting, next to core's sanitization options on Configuration > Media > File system.
  • Uploaded filenames longer than the limit are shortened before their extension, and trailing dots, dashes, underscores and spaces are trimmed.
  • The limit applies after the File module options (transliteration, lowercase, etc.) and before core's security rules, so dangerous extensions are still handled.
  • A Drush command, filename-max-length:rename, renames existing files according to all the current sanitization options, as if they were uploaded today.

Requirements

  • Drupal 11.3 or later, or Drupal 12.
  • The core File module.
  • Drush 13.7 or later, only for the rename command.

Configuration

Go to Configuration > Media > File system (/admin/config/media/file-system) and set the Maximum filename length in the Sanitize filenames section. The length counts characters, extension included. 0 means no limit, and the maximum is 240, the limit of core's FileNameLength constraint.

Core's security rules may still add a few characters (.txt, _). When a file with the same name already exists, core adds a numeric suffix (_0, _1…), which can also exceed the limit.

Renaming existing files

# List the files that would be renamed.
drush filename-max-length:rename --dry-run

# Rename them. Back up the files and the database first.
drush filename-max-length:rename --yes

The command renames every permanent managed file whose name is not compliant. These keep working after the rename:

  • file and image fields, on nodes as on media, which reference files by ID;
  • images inserted in formatted text through CKEditor 5, whose src is rewritten by the editor_file_reference filter;
  • image styles: the derivatives of the old name are flushed.

These are not updated:

  • hard-coded links to the old file URLs, in formatted text or outside the site;
  • unmanaged files.
Supporting organizations: 

Project information

  • Project categories: Media
  • Created by pogfra on , updated
  • shield alertThis project is not covered by the security advisory policy.
    Use at your own risk! It may have publicly disclosed vulnerabilities.

Releases