Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/File/file.api.php \hook_file_download()
  2. 4.7.x developer/hooks/core.php \hook_file_download()
  3. 5.x developer/hooks/core.php \hook_file_download()
  4. 7.x modules/system/system.api.php \hook_file_download()
  5. 8.9.x core/lib/Drupal/Core/File/file.api.php \hook_file_download()
  6. 9 core/lib/Drupal/Core/File/file.api.php \hook_file_download()

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 modules/upload/upload.module
Implementation of hook_file_download().
user_file_download in modules/user/user.module
Implementation of hook_file_download().
1 invocation of hook_file_download()
file_download_headers in includes/file.inc
Retrieves headers for a private file download.

File

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;
    }
  }
}