Moving public files to private

Public

Moves al file entities which are public to private. Could also be reverted and ad search conditions. Inspired by file edit function.

</> CopyGet raw version
php
  1. $query = new EntityFieldQuery();
  2. $name_search = 'test';
  3.  
  4. $query->entityCondition('entity_type', 'file')
  5. ->propertyCondition('filename', '%' . $name_search . '%', LIKE);
  6.  
  7. $result = $query->execute();
  8.  
  9. if (isset($result['file'])) {
  10. $file_nids = array_keys($result['file']);
  11. $files = entity_load('file', $file_nids);
  12. }
  13.  
  14. foreach($files as $file){
  15. if(file_uri_scheme($file->uri) == 'public') {
  16. $file_destination = file_stream_wrapper_uri_normalize('private://' . file_uri_target($file->uri));
  17. if ($moved_file = file_move($file, $file_destination, FILE_EXISTS_RENAME)) {
  18. // Only re-assign the file object if file_move() did not fail.
  19. $file = $moved_file;
  20. }
  21. drupal_set_message(file_save($file));
  22. }
  23. }