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