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.


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)

  • 2
    For private downloads need file_create_url() and hook_file_download(). This works well. – Johnny Dec 25 '11 at 13:25

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;

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, privacy policy and cookie policy

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