The File (Field) Paths module extends the default functionality of Drupal's core File module, Image module and many other File upload modules, by adding the ability to use entity based tokens in destination paths and file names.

In simple terms, File (Field) Paths allows you to automatically sort and rename your uploaded files using token based replacement patterns to maintain a nice clean filesystem.

 

Features:

  • Configurable file paths using entity tokens as well as the global tokens core already offers.
  • Configurable file names.
    • A generated name always ends in an extension the field allows. When the pattern does not give one, the file's own extension is added.
    • Generated names are sanitised the way core sanitises an uploaded file name, with the same file system settings.
  • Support for file based fields, including but not limited to:
    • Drupal core File module
    • Drupal core Image module
    • Contributed field types built on the core File field, such as Video
  • File path and filename cleanup options:
    • Remove slashes from tokens.
    • Filter out words and punctuation by taking advantage of the Pathauto module.
    • Transliterate unicode characters into US-ASCII.
  • File tokens of its own: [file:ffp-name-only], [file:ffp-name-only-original] and [file:ffp-extension-original].
  • Retroactive updates - rename and/or move previously uploaded files, from the field settings form or with drush filefield_paths:update, and report any file that could not be moved.
  • Active updating - actively rename and/or move previously uploaded files each time their entity is saved.
  • Create Redirect - automatically create a redirect when moving uploaded files, using the Redirect module.

 

Requirements:

  • Drupal 10.3 or 11

 

Recommended Modules:

  • Token - the token browser on the field settings form, and many more entity and field tokens.
  • Pathauto - the "Cleanup using Pathauto" option.
  • Redirect - the "Create Redirect" option.

 

Usage/Configuration:

Once installed, File (Field) Paths needs to be configured for each file field you wish to use. Settings can be found on the settings form of any supported file based field.

Example: Administration > Structure > Content types > Article > Manage fields > Image
/admin/structure/types/manage/article/fields/node.article.field_image

With "Enable File (Field) Paths?" ticked, the core "File directory" setting is replaced by a token pattern for the file path and one for the file name, each with its own cleanup options. The patterns are resolved when the entity is saved.

Temporary file location

Until the entity is saved, each uploaded file waits in a temporary location. A new install uses temporary://filefield_paths. Keep it: files there have no public URL, and image previews still work. If temporary:// does not suit your server, such as several web servers without a shared temporary directory, set the "Temporary file location" to private://filefield_paths on the File (Field) Paths settings page (/admin/config/media/file-system/filefield-paths), or per field on the field settings form. The status report warns when the temporary file location points at public:// while a safer scheme is available.

The README covers the private file system setup step by step.

 

Frequently Asked Questions:

  • Doesn't the core File directory setting take tokens already?
    It does. Core resolves them at upload time, before the entity exists, so only global tokens such as the date or the current user have a value. File (Field) Paths resolves its patterns when the entity is saved, so the entity's ID, title, dates and fields are all available.
  • Why aren't my files in the folder I configured?
    The tokens are resolved when the entity is saved, and the file moves to its final path at that point. Until then it waits in the temporary file location. A file that stays there belongs to an entity that was never saved, and core's cron removes temporary files that were never made permanent.
  • Why is there a warning on the 'Retroactive updates' feature?
    A retroactive update goes through every entity of the bundle and moves and/or renames its files. Anything that linked to an old path breaks unless "Create Redirect" is on, and a large bundle makes for a long batch. Try it on a copy of the site first.

 

Supporting organizations: 

Project information

Releases