Sometimes 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:
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; continue; } $file->filesize = filesize($target); if ($file->filesize > 0) { file_save($file); } 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 $>
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:
- Fix the import script to properly move the files directory during upgrade (simple fix once found).
- Run this script after moving the files directory to the new location.
Looking for quality web hosting? Look no further than Arvixe Web Hosting!