Last active
January 31, 2020 02:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.)