function hook_file_download

7.x system.api.php hook_file_download($uri)
6.x core.php hook_file_download($filepath)

Control access to private file downloads and specify HTTP headers.

This hook allows modules enforce permissions on file downloads when the private file download method is selected. Modules can also provide headers to specify information like the file's name or MIME type.

Parameters

$filepath: String of the file's path.

Return value

If the user does not have permission to access the file, return -1. If the user has permission, return an array with the appropriate headers. If the file is not controlled by the current module, the return value should be NULL.

Related topics

2 functions implement hook_file_download()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

upload_file_download in drupal-6.x/modules/upload/upload.module
Implementation of hook_file_download().
user_file_download in drupal-6.x/modules/user/user.module
Implementation of hook_file_download().
1 invocation of hook_file_download()
file_download in drupal-6.x/includes/file.inc
Call modules that implement hook_file_download() to find out if a file is accessible and what headers it should be transferred with. If a module returns -1 drupal_access_denied() will be returned. If one or more modules returned headers the download…

File

documentation-6.x/developer/hooks/core.php, line 536
These are the hooks that are invoked by the Drupal core.

Code

function hook_file_download($filepath) {
  if ($filemime = db_result(db_query("SELECT filemime FROM {fileupload} WHERE filepath = '%s'", file_create_path($filepath)))) {
    if (user_access('access content')) {
      return array('Content-type:' . $filemime);
    }
    else {
      return -1;
    }
  }
}