Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save franxo/4b8d0542d4fb4596ccc1334bd599f17d to your computer and use it in GitHub Desktop.
Save franxo/4b8d0542d4fb4596ccc1334bd599f17d to your computer and use it in GitHub Desktop.
Drupal 8: Move files from custom module to private files directory

Drupal 8: Move files from custom module to private files directory

```
<?php
/**
* @file
* Install, update, and uninstall functions for my_module.
*/
/**
* Copies file to the private files folder.
*/
function my_module_install() {
// Create the example file directory and ensure it's writable.
$directory = 'private://files/my_module';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
// Copy the example file to example directory.
$module_path = drupal_get_path('module', 'my_module');
$file_source = $module_path . '/data/example.xml';
file_unmanaged_copy($file_source, $directory . '/example.xml', FILE_EXISTS_REPLACE);
}
/**
* Removes the private files folder
*/
function my_module_uninstall() {
$directory = 'private://files/my_module';
file_unmanaged_delete_recursive($directory);
}
```
* Uncomment line
```
$settings['file_private_path'] = '';
```
* Add folder location (A relative path can be used)
```
$settings['file_private_path'] = '../private';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment