Skip to main content
drupalreleases
Release: Drupal 11.4.9 — Update released for Drupal core (11.4.9)! Release: Better Exposed Filters 7.2.0 — Minor update available for module better_exposed_filters (7.2.0). Release: Bootstrap 8.x-3.43 — Minor update available for theme bootstrap (8.x-3.43). Release: Layout Paragraphs 2.1.5 — Minor update available for module layout_paragraphs (2.1.5). Release: Layout Paragraphs 3.0.0 — Major update available for module layout_paragraphs (3.0.0). Release: Book 3.0.8 — Minor update available for module book (3.0.8). Release: Tagify 2.0.5 — Minor update available for module tagify (2.0.5). Release: Editoria11y Accessibility Checker 3.0.11 — Minor update available for module editoria11y (3.0.11). Module Revived: @font-your-face 4.3.0 — Module fontyourface updated after 7 months of inactivity (4.3.0). Security Coverage: Open Y Branch Selector — Module openy_branch_selector now has official Drupal security advisory coverage.

File Field Replace

651 sites Security covered Drupal 9–11
View on drupal.org

This module allows you to choose how to handle uploaded files that share a name with an existing file. You can select to rename the new file, replace the existing one, or prevent the upload altogether.

This module adds a setting to file fields to support selecting the method handling when a file already exists with the same name as the uploaded file. The options are:

  • Rename the file (this is the default value and the default behaviour of file fields)
  • Replace the existing file
  • Prevent the file from being uploaded (will display an error message)

The main reason for creating this module was to support replacing existing files instead of renaming, hence the name.

Since the core file field form element uses the managed_file forms API element, we've extended that to include a managed_file_plus element that supports specifying the file handling method with the #upload_replace property. This new managed_file_plus element is also available for use in custom form code.

Caveats

There are a few things to think about when selecting the 'Replace' option:

Potential Data Loss

Depending on your site setup, there is a potential for accidental data loss. A couple examples are:

  • You have two separate file fields setup to replace files and save to the same destination directory (e.g. sites/default/files). Editor A uploads readme.txt to field A. Then, Editor B comes along and uploads readme.txt to field B. Editor A's file has now been overwritten and the original lost.
  • You have a single managed_file with multiple usages (e.g. referenced by two different nodes). If you replace the file it will be replaced in all usages which may not be the intended result.

Revisions

Files are just replaced in the filesystem completely, and the existing managed_file entry is continued to be used. This means that there will be no revision history of the old file.

Caching

For any servers running caching layers, the file url will need to be flushed manually. Additionally, browsers may also have cached the file and will serve the old file until it expires or is manually cleared by the user.

Alternatives

FileField Paths & Redirect

If you have the redirect module enabled, the filefield_paths module provides an option to create redirects from the old file to the new file when uploading. For some use-cases this may be more optimal as the new filenames can contain revision numbers, but the old files won't be accessible anymore because of the redirects.

Core Media & Media Entity Download

If you're using the core media module, the media_entity_download provides a canonical media download route that you can use instead of linking directly to the file.

Depends on

Dependencies of the latest stable release

  • file Drupal core

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Oct 2023
Latest release
1 month ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Release Timeline

Releases

Version Type Core Release date
3.1.0 Stable 9–11 Aug 25, 2026
3.0.0 Stable 9–10 Oct 11, 2023
3.x-dev Dev 9–11 Oct 11, 2023