6

I need a function in my module for users to download a file from a private filesystem.

$file : "private://sample.csv"

Which function should be used, file_download(), or file_transfer()?

function file_download_page() {
  $file = "private://sample.csv";
  //code for downloading $file
}    

Please provide sample code.

3 Answers 3

6

You just need to call file_create_url().

It will return an URL that will go through Drupal which will then check the appropriate permiss (if you need custom checks, you need to implement hook_file_download)

1
  • 2
    For private downloads need file_create_url() and hook_file_download(). This works well.
    – Johnny
    Commented Dec 25, 2011 at 13:25
1

I use hook_file_download_access:

/**
 * Implement hook_file_download_access().
 */
function YOURMODULENAME_file_download_access($file_item, $entity_type, $entity) {
  global $user;
  return (in_array('hr', $user->roles) || in_array('administrator', $user->roles)) ? TRUE : FALSE;
}
0

Using DownloadFile you can download the file. I am using this module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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