Have you ever come across the situation that you are unsure if the user-uploaded files Drupal knows about are in sync with what's on your servers harddrive?
I have.

A simple script that you can put in a .php file in the root of your website:


?php

/**
 * @file
 * Simple check if files that reside in the file_managed table
 * really exist on file storage
 */

/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Get all file entries from database
$files = db_query('SELECT uri  FROM {file_managed}');

// Loop file entries
foreach($files as $file){
  // convert to absolute server path
  $path = drupal_realpath($file->uri);

  // Check existence and report per file to screen
  if (file_exists($path)) {
    echo $file->uri." | The file exists <br /> \n";
  } 
  else {
    echo $file->uri." | The file does not exist <br /> \n";
  }
}

Or adjust it to your needs to build a cron-hooked module that reports errors to your watchdog ;-)

Add new comment