Skip to content

Instantly share code, notes, and snippets.

@jackaponte
Last active January 31, 2020 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackaponte/c053c4aa201fbf948698d519b07c87e2 to your computer and use it in GitHub Desktop.
Save jackaponte/c053c4aa201fbf948698d519b07c87e2 to your computer and use it in GitHub Desktop.
Scan directories for files and add them to the Backdrop managed file system
<?php
// Load file from disk.
echo 'Managing unmanaged files:\n';
// Change the DIRECTORY_NAME below for each directory you'd like to process.
foreach (new DirectoryIterator('DIRECTORY_NAME') as $unmanaged_file) {
if(($unmanaged_file->isDir()) | ($unmanaged_file->isDot()) | (!$unmanaged_file->valid())) continue;
$filename = $unmanaged_file->getFilename();
if($filename == '.htaccess') continue;
$pathname = $unmanaged_file->getPathname();
echo $pathname. "\n";
$contents = file_get_contents($pathname);
// Make sure to set the "public://" bit below to the full public:// path of the directory you're processing
$file = file_save_data($contents, 'public://' . $filename , FILE_EXISTS_REPLACE);
file_usage_add($file, 'user', 'user', '1');
$count++;
}
print_r('Managed ' . $count . ' files!');
@jackaponte
Copy link
Author

Run this with drush: drush scr --user=1 manage_unmanaged_files.php

@jackaponte
Copy link
Author

jackaponte commented Jan 31, 2020

Tried this today, nearly three years after writing it, and it totally works! (So long as you are sure to set the public:// bit correctly, tried to quickly note that in the code though I know it's not the clearest.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment