Drupal 7: Custom Script to fix Media Files Filesize

David G - DrupalSometimes when working with Drupal everything is not a walk in the park. Even I run into oddities, or gotchas that cause me some headache. I love it when I’m able to come up with a small script that fixes my problems with little hassle. Recently in a site upgrade from Drupal 6 to Drupal 7 I was adding the Media module to my website and noticed my files listing showed many 0 byte files. I wanted to fix this error for files that do exist on the file-system so the client would not freak out with red error messages and warning colors all over the website!

After upgrading a site (and moving a Drupal 6 /sites/all/* single drupal installation into a proper /sites/SITENAME/* multi-site install) the files directory wasn’t moved properly. Running a full upgrade script to fix the issue takes minutes, which was not feasible. So after upgrading the site to Drupal 7 the Media Files listing appears as:

The files were not found on disk, but they exist in the database ok.

The files were not found on disk, but they exist in the database ok.

Even after moving the files so they exist in the subsites files directory nearly every file reads 0 bytes for the filesize attribute. If the row is red then the file is not found on disk.

I then wrote a small script to fix this issue using Drush:

$fids = db_query('SELECT fid FROM {file_managed} WHERE filesize = 0')->fetchCol();

foreach(file_load_multiple($fids) as $file) {
  $target = drupal_realpath($file->uri);
  if (!file_exists($target)) {
    echo "File $file->uri does not found." .PHP_EOL;
    $file->filesize = filesize($target);
    if ($file->filesize > 0) {
    else {
      echo "Size of $file->uri is zero." . PHP_EOL;

By running this file with drush php-script amend-media-filesizes.php we can then reload the Admin -> Content -> Files page to see our changes:

/var/www/drupal/sites/subsite1$> drush php-script fix.php 
File public://efiles/New_Student_Info_Fall2009.pdf does not exist
After running our script the filesize values exist.

After running our script the filesize values exist.

I hope you can see using Drush, a small piece of the Drupal 7 API can help fix random problems you may encounter when using Drupal. This error actually was “human error” … I actually had an error in my upgrade script that did not move the files directory properly to a new location on the webserver. When the Media module is installed in the upgrade process it cannot see the files. Simply restoring the files will not set these value either; you must either:

  1. Fix the import script to properly move the files directory during upgrade (simple fix once found).
  2. Run this script after moving the files directory to the new location.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *