0

My default download method is public, but I need save some file on private folder, I am doing this:

//...
$file = file_save_data($handle, 'private://example.zip', FILE_EXISTS_REPLACE);
//...

Now when I try show to download it:

//...
$file = file_load($file_fid);
$url = file_create_url($file->uri);

$output['download'] = [
  '#type' => 'markup',
  '#markup' => l(t('Download'), $url),
];

return $output;
//...

If I do click on the url, I get:

Access denied. You are not authorized to access this page.

I am superadmin (uid = 1). How I can management those files for me and for others users?

  • Private files are not meant to be used like that. Check your docs – sareed Aug 28 '15 at 16:58
0

For bypass Drupal permission, you can implements hook_file_download():

/**
 * Implements hook_file_download().
 */
function mymodule_file_download($uri) {
  if (someCondition()) {
    if (!user_access('some permissions')) {
      // Access to the file is denied.
      return -1;
    }
    else {
      return array('Content-Type' => file_get_mimetype($uri));
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.