Private File Management

Public

This snippet allow Owner and a Role to access to privates files

</> CopyGet raw version
php
  1. <?php
  2.  
  3. function MYMODULENAME_file_download($uri) {
  4. global $user;
  5.  
  6. $files = file_load_multiple(array(), array('uri' => $uri));
  7. $file = reset($files); // If empty, $file will be false, otherwise will contain the required file
  8.  
  9. $has_access = FALSE;
  10. if ($file) {
  11. if (user_access('view user private_documents')) {
  12. $has_access = TRUE;
  13. }
  14. elseif ($user->uid == $file->uid) {
  15. $has_access = TRUE;
  16. } else {
  17. $has_access = FALSE;
  18. }
  19. }
  20.  
  21. // Authorize download
  22. if ($has_access) {
  23. return array('Content-Type' => file_get_mimetype($uri));
  24. }
  25. else {
  26. return -1;
  27. }
  28. }