Skip to main content

Have you had the situation where you needed to compare the files recorded in the database (e.g., file_managed table) against the actual files in the file system directory (sites/default/files/...). This is useful for scenarios where you want to ensure that the database records and the physical files are in sync.  To do this perform the following steps:

Retrieve File Information from the Database

Use the Entity API or direct database queries to fetch information about files from the file_managed table or the appropriate file entity table.

Check File Existence in the File System

Use the file system functions in PHP to check whether the files recorded in the database actually exist in the file system.

use Drupal\file\FileStorageInterface;

// Get the file storage service.
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
// Query all file entities.
$files = $file_storage->loadMultiple();
foreach ($files as $file) {
 // Get the file URI.
 $uri = $file->getFileUri();
 // Check if the file exists in the file system.
 if (file_exists($uri)) {
   // File exists in the file system.
   // Additional checks or processing can be added here.
 }
 else {
   // File is recorded in the database but does not exist in the file system.
   // Handle accordingly (e.g., delete the database record).
 }
}

Note that this is a basic example, and depending on your use case, you might want to add more checks or processing steps. For instance, you may want to compare file sizes, timestamps, or other attributes to ensure the integrity of the files.

Keep in mind that the above example assumes a default file system setup in Drupal. If you're using a custom file system or a different file entity setup, you may need to adjust the code accordingly. Additionally, always make sure to perform such operations with caution, especially when dealing with file deletions.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...