Skip to content

Instantly share code, notes, and snippets.

@wojtha
Created May 13, 2011 01:04
Show Gist options
  • Save wojtha/969769 to your computer and use it in GitHub Desktop.
Save wojtha/969769 to your computer and use it in GitHub Desktop.
Replace non-existent files with empty file using Drupal {file} table.
// REPLACE?
$replace = FALSE;
// path to empty file
$source = 'files/empty.png';
// RUN
$result = db_query('SELECT fid, filename, filepath FROM {files}');
$src = file_create_path($source);
$src_exists = file_exists($src);
$i = 1;
while ($f = db_fetch_object($result)) {
$dest = file_create_path($f->filepath);
if (!file_exists($dest)) {
$out .= $i++ . '. ' . $f->filepath . "\n";
if ($replace && $src_exists) {
copy($src, $dest);
chmod($dest, 0664);
db_query("UPDATE {files} SET origname='%s' WHERE fid=%d", 'empty.png', $f->fid);
}
}
}
return $out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment